Module: DeepNest
- Defined in:
- lib/deep_nest.rb,
lib/deep_nest/helpers.rb,
lib/deep_nest/version.rb
Overview
Namespace for helper methods to use self in recursive methods
Defined Under Namespace
Modules: Helpers Classes: Error
Constant Summary collapse
- VERSION =
'0.1.2'
Class Method Summary collapse
-
.deep_dup(structure) ⇒ Hash, Array
Returns a deep copy of the passed hash or array.
-
.deep_equal?(struct1, struct2) ⇒ true, false
Returns true if the passed parameters are same in structure and values, false otherwise.
-
.deep_merge(hash1, hash2) {|&block| ... } ⇒ Hash
Returns a hash with the passed hashes recursively merged.
-
.deep_stringify_keys(structure) ⇒ Hash, Array
Returns a hash or array with all hash keys converted to strings.
-
.deep_symbolize_keys(structure) ⇒ Hash, Array
Returns a hash or array with all hash keys converted to symbols.
-
.deep_transform_keys(structure) {|&block| ... } ⇒ Hash, Array
Returns a hash or array with all hash keys modified by the passed block.
-
.deep_transform_values(structure) {|&block| ... } ⇒ Hash, Array
Returns a hash or array with all hash values modified by the passed block.
Class Method Details
.deep_dup(structure) ⇒ Hash, Array
Returns a deep copy of the passed hash or array.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/deep_nest.rb', line 19 def deep_dup(structure) case structure when Array structure.map { |x| deep_dup(x) } when Hash structure.transform_values { |v| deep_dup(v) } else structure.dup end end |
.deep_equal?(struct1, struct2) ⇒ true, false
Returns true if the passed parameters are same in structure and values, false otherwise.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/deep_nest.rb', line 64 def deep_equal?(struct1, struct2) if struct1.eql?(struct2) case struct1 when Array struct1.zip(struct2).each { |e1, e2| deep_equal?(e1, e2) } when Hash struct1.merge(struct2).each { |v1, v2| deep_equal?(v1, v2) } else true end else false end end |
.deep_merge(hash1, hash2) {|&block| ... } ⇒ Hash
Returns a hash with the passed hashes recursively merged. An optional block can be passed to merge values.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/deep_nest.rb', line 40 def deep_merge(hash1, hash2, &block) raise Error, 'Parameters must be hashes' unless hash1.is_a?(Hash) && hash2.is_a?(Hash) hash1.merge(hash2) do |k, v1, v2| if v1.is_a?(Hash) && v2.is_a?(Hash) deep_merge(v1, v2, &block) elsif block_given? block.call(k, v1, v2) else v2 end end end |
.deep_stringify_keys(structure) ⇒ Hash, Array
Returns a hash or array with all hash keys converted to strings.
125 126 127 |
# File 'lib/deep_nest.rb', line 125 def deep_stringify_keys(structure) deep_transform_keys(structure, &:to_s) end |
.deep_symbolize_keys(structure) ⇒ Hash, Array
Returns a hash or array with all hash keys converted to symbols.
135 136 137 |
# File 'lib/deep_nest.rb', line 135 def deep_symbolize_keys(structure) deep_transform_keys(structure, &:to_sym) end |
.deep_transform_keys(structure) {|&block| ... } ⇒ Hash, Array
Returns a hash or array with all hash keys modified by the passed block.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/deep_nest.rb', line 87 def deep_transform_keys(structure, &block) case structure when Hash structure.each_with_object({}) do |(k, v), result| result[yield(k)] = deep_transform_keys(v, &block) end when Array structure.map { |e| deep_transform_keys(e, &block) } else structure end end |
.deep_transform_values(structure) {|&block| ... } ⇒ Hash, Array
Returns a hash or array with all hash values modified by the passed block.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/deep_nest.rb', line 108 def deep_transform_values(structure, &block) case structure when Hash structure.transform_values { |v| deep_transform_values(v, &block) } when Array structure.map { |e| deep_transform_values(e, &block) } else yield(structure) end end |