项目作者: rchampourlier

项目描述 :
A Ruby library of functions to access and manipulate hash data structures.
高级语言: Ruby
项目地址: git://github.com/rchampourlier/hash_op.git
创建时间: 2015-06-12T22:01:23Z
项目社区:https://github.com/rchampourlier/hash_op

开源协议:

下载


HashOp

A Ruby library of functions to access and manipulate hash data structures.

Build Status
Code Climate
Coverage Status

Installation

Add this line to your application’s Gemfile:

  1. gem 'hash_op'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install hash_op

Usage

Available operations

Deep Access

  1. HashOp::Deep.fetch({a: {b: {c: 1}}}, :'a.b.c')
  2. => 1
  3. HashOp::Deep.merge({ a: { b: { c: 1 } } }, :'a.b.c', 2)
  4. => {
  5. :a => {
  6. :b => {
  7. :c => 2
  8. }
  9. }
  10. }

Filter

  1. hashes = [
  2. { value: 123, regexp: "itsamatch", proc: "1+1" },
  3. { value: 123, regexp: "abcdef", proc: "1+2" },
  4. { value: 234, regexp: "abcdef", proc: "1+2" }
  5. ]
  6. criteria = {
  7. path: :value,
  8. matching_object: 123
  9. }
  10. HashOp::Filter.filter(hashes, { value: 123 })
  11. => [
  12. [0] {
  13. :proc => "1+1",
  14. :regexp => "itsamatch",
  15. :value => 123
  16. },
  17. [1] {
  18. :proc => "1+2",
  19. :regexp => "abcdef",
  20. :value => 123
  21. }
  22. ]
  23. HashOp::Filter.filter(hashes, { value: 123, regexp: /match/ })
  24. => [
  25. [0] {
  26. :proc => "1+1",
  27. :regexp => "itsamatch",
  28. :value => 123
  29. }
  30. ]
  31. HashOp::Filter.filter(hashes, { proc: ->(x) { eval(x) == 2 } })
  32. => [
  33. [0] {
  34. :proc => "1+1",
  35. :regexp => "itsamatch",
  36. :value => 123
  37. }
  38. ]
  39. Internally, `HashOp::Filter::filter` uses
  40. `HashOp::Filter::match?(hash, criteria)` which you can
  41. use too.

Mapping

  1. hash = {a: { b: { c: 1 } } }
  2. mapping = { r: { path: :'a.b.c' } }
  3. HashOp::Mapping.apply_mapping(hash, mapping)
  4. => {
  5. :r => 1
  6. }
  7. hash = {
  8. raw: { deep: 'raw_value' },
  9. time: '2015-07-06 03:37:13 +0200',
  10. mapped_hash: {
  11. raw: { deep: 'deep_raw_value' },
  12. time: '2014-07-06 03:37:13 +0200'
  13. },
  14. parseable_string: 'a=1;b=2;t=2013-07-06 03:37:13 +0200',
  15. array: [
  16. '2015-07-06 03:37:13 +0200',
  17. '2014-07-06 03:37:13 +0200',
  18. '2013-07-06 03:37:13 +0200'
  19. ]
  20. }
  21. mapping = {
  22. raw: { path: :'raw.deep' },
  23. time: { path: :time, type: :time },
  24. raw_from_mapped_hash: {
  25. path: :'mapped_hash.raw.deep',
  26. },
  27. time_from_mapped_hash: {
  28. path: :'mapped_hash.time',
  29. type: :time
  30. },
  31. values_from_parseable_string: {
  32. path: :parseable_string,
  33. type: :parseable_string,
  34. parsing_mapping: {
  35. value: { regexp: 'a=(\d)+;' },
  36. time: {
  37. regexp: 't=(.*)$',
  38. type: :time
  39. }
  40. }
  41. },
  42. times_from_array: {
  43. type: :array,
  44. path: :array,
  45. item_mapping: { type: :time }
  46. }
  47. }
  48. HashOp::Mapping.apply_mapping(hash, mapping)
  49. => {
  50. :raw => "raw_value",
  51. :raw_from_mapped_hash => "deep_raw_value",
  52. :time => 2015-07-06 03:37:13 +0200,
  53. :time_from_mapped_hash => 2014-07-06 03:37:13 +0200,
  54. :times_from_array => [
  55. [0] 2015-07-06 03:37:13 +0200,
  56. [1] 2014-07-06 03:37:13 +0200,
  57. [2] 2013-07-06 03:37:13 +0200
  58. ],
  59. :values_from_parseable_string => {
  60. :time => 2013-07-06 03:37:13 +0200,
  61. :value => "1"
  62. }
  63. }

Grouping

  1. hashes = [
  2. {
  3. grouping_path: 'A',
  4. value: 1,
  5. node: { 'deep_grouping_path': 'AA' }
  6. },
  7. {
  8. grouping_path: 'B',
  9. value: 2,
  10. node: { 'deep_grouping_path': 'BB' }
  11. },
  12. {
  13. grouping_path: 'A',
  14. value: 3,
  15. node: { 'deep_grouping_path': 'AB' }
  16. },
  17. {
  18. grouping_path: 'A',
  19. value: 4,
  20. node: { 'deep_grouping_path': 'AA' }
  21. }
  22. ]
  23. HashOp::Grouping.group_on_path(hashes, :grouping_path)
  24. => {
  25. "A" => [
  26. [0] {
  27. :grouping_path => "A",
  28. :node => {
  29. :deep_grouping_path => "AA"
  30. },
  31. :value => 1
  32. },
  33. [1] {
  34. :grouping_path => "A",
  35. :node => {
  36. :deep_grouping_path => "AB"
  37. },
  38. :value => 3
  39. },
  40. [2] {
  41. :grouping_path => "A",
  42. :node => {
  43. :deep_grouping_path => "AA"
  44. },
  45. :value => 4
  46. }
  47. ],
  48. "B" => [
  49. [0] {
  50. :grouping_path => "B",
  51. :node => {
  52. :deep_grouping_path => "BB"
  53. },
  54. :value => 2
  55. }
  56. ]
  57. }
  58. HashOp::Grouping.group_on_path(hashes, :'node.deep_grouping_path')
  59. => {
  60. "AA" => [
  61. [0] {
  62. :grouping_path => "A",
  63. :node => {
  64. :deep_grouping_path => "AA"
  65. },
  66. :value => 1
  67. },
  68. [1] {
  69. :grouping_path => "A",
  70. :node => {
  71. :deep_grouping_path => "AA"
  72. },
  73. :value => 4
  74. }
  75. ],
  76. "AB" => [
  77. [0] {
  78. :grouping_path => "A",
  79. :node => {
  80. :deep_grouping_path => "AB"
  81. },
  82. :value => 3
  83. }
  84. ],
  85. "BB" => [
  86. [0] {
  87. :grouping_path => "B",
  88. :node => {
  89. :deep_grouping_path => "BB"
  90. },
  91. :value => 2
  92. }
  93. ]
  94. }
  95. HashOp::Grouping.group_on_paths(hashes, [:grouping_path, :'node.deep_grouping_path'])
  96. => {
  97. "A" => {
  98. "AA" => [
  99. [0] {
  100. :grouping_path => "A",
  101. :node => {
  102. :deep_grouping_path => "AA"
  103. },
  104. :value => 1
  105. },
  106. [1] {
  107. :grouping_path => "A",
  108. :node => {
  109. :deep_grouping_path => "AA"
  110. },
  111. :value => 4
  112. }
  113. ],
  114. "AB" => [
  115. [0] {
  116. :grouping_path => "A",
  117. :node => {
  118. :deep_grouping_path => "AB"
  119. },
  120. :value => 3
  121. }
  122. ]
  123. },
  124. "B" => {
  125. "BB" => [
  126. [0] {
  127. :grouping_path => "B",
  128. :node => {
  129. :deep_grouping_path => "BB"
  130. },
  131. :value => 2
  132. }
  133. ]
  134. }
  135. }

See specs for more details on each operation and operations not documented here.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rchampourlier/hash_op. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Revisions

0.3.0

  • Added Read.values_at_path
  • Minor fixes

0.2.0

  • Removed some operations that made no real sense (Math.sum and Math.sum_two).
  • Renamed DeepAccess to Deep.
  • Renamed Merge.merge to Merge.flat and Merge.merge_by_group to Merge.by_group.

0.1.0

Initial version