Class: NiceHash
- Defined in:
- lib/nice_hash.rb,
lib/nice/hash/diff.rb,
lib/nice/hash/generate.rb,
lib/nice/hash/validate.rb,
lib/nice/hash/deep_clone.rb,
lib/nice/hash/get_values.rb,
lib/nice/hash/select_key.rb,
lib/nice/hash/set_nested.rb,
lib/nice/hash/set_values.rb,
lib/nice/hash/transtring.rb,
lib/nice/hash/nice_filter.rb,
lib/nice/hash/flatten_keys.rb,
lib/nice/hash/get_all_keys.rb,
lib/nice/hash/delete_nested.rb,
lib/nice/hash/select_fields.rb,
lib/nice/hash/pattern_fields.rb,
lib/nice/hash/change_one_by_one.rb,
lib/nice/hash/compare_structure.rb
Overview
NiceHash creates hashes following certain patterns so your testing will be much easier. You can easily generates all the hashes you want following the criteria you specify. Many other features coming to Hash class like the methods 'bury' or select_key, access the keys like methods: my_hash.my_key.other_key. You will be able to generate thousands of different hashes just declaring one and test easily APIs based on JSON for example. To generate the strings following a pattern take a look at the documentation for string_pattern gem: https://github.com/MarioRuiz/string_pattern This is the Hash we will be using on the examples declared on the methods source code documentation:
my_hash={
name: 'Peter',
address: {wrong: '#$$$$$', correct: :'10-30:L_'},
city: {wrong: 'Germany', correct: :'Madrid|Barcelona|London|Akureyri'},
products: [
{
name: :'10:Ln',
price: {wrong: '-20', correct: :'1-10:N'}
},
{
name: :'10:Ln',
price: {wrong: '-20', correct: :'1-10:N'}
},
]
}
Class Attribute Summary collapse
-
.values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
-
.deep_clone(obj) ⇒ Object
Deep clones the supplied object.
-
.delete_nested(hash, nested_key) ⇒ Object
Deletes the supplied nested key.
-
.diff(expected, actual, prefix = "") ⇒ Hash
Compares two hashes and returns differences with dot-notation paths.
-
.flatten_keys(hash, prefix = "") ⇒ Hash
Flattens a nested hash so keys become dot-notation strings.
-
.get_all_keys(h) ⇒ Object
Get all the keys of a hash.
-
.nice_filter(hash, keys) ⇒ Object
Filter the hash supplied and returns only the specified keys.
-
.set_nested(hash, nested_key, value, only_if_exist = false) ⇒ Object
sets the supplied value on the supplied nested key.
-
.transtring(hash) ⇒ Object
Translate a hash of hashes into a string separted by .
-
.unflatten_keys(hash) ⇒ Hash
Unflattens a hash with dot-notation keys into a nested hash.
Class Attribute Details
.values ⇒ Object (readonly)
Returns the value of attribute values.
50 51 52 |
# File 'lib/nice_hash.rb', line 50 def values @values end |
Class Method Details
.deep_clone(obj) ⇒ Object
Deep clones the supplied object
@param obj [Object] The object we want to deep clone
@return [Object]
@example my_hash = { user: { address: { city: 'Madrid', country: 'Spain' }, name: 'Peter', age: 33 }, customer: true } NiceHash.deep_clone(my_hash) #>:country=>"Spain", :name=>"Peter", :age=>33}, :customer=>true}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nice/hash/deep_clone.rb', line 23 def self.deep_clone(obj) return obj.clone unless obj.is_a?(Array) || obj.is_a?(Hash) obj.clone.tap do |new_obj| if new_obj.is_a?(Array) new_obj.each_with_index do |val, i| new_obj[i] = deep_clone(val) end elsif new_obj.is_a?(Hash) new_obj.each do |key, val| new_obj[key] = deep_clone(val) end end end end |
.delete_nested(hash, nested_key) ⇒ Object
Deletes the supplied nested key
@param hash [Hash] The hash we want @param nested_key [Hash] [String] String with the nested key: 'uno.dos.tres' or a hash { uno: :tres }
@return [Hash]
@example my_hash = { user: { address: { city: 'Madrid', country: 'Spain' }, name: 'Peter', age: 33 }, customer: true } NiceHash.delete_nested(my_hash, 'user.address.city') #>:user=>{:address=>{:country=>"Spain", :name=>"Peter", :age=>33}, :customer=>true}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nice/hash/delete_nested.rb', line 24 def self.delete_nested(hash, nested_key) nested_key = transtring(nested_key) keys = nested_key.split(".") if keys.size == 1 hash.delete(nested_key.to_sym) else parent = hash keys[0..-2].each do |k| sym = k.to_sym return hash unless parent.is_a?(Hash) && parent.key?(sym) parent = parent[sym] end parent.delete(keys[-1].to_sym) if parent.is_a?(Hash) end return hash end |
.diff(expected, actual, prefix = "") ⇒ Hash
Compares two hashes and returns differences with dot-notation paths.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/nice/hash/diff.rb', line 19 def self.diff(expected, actual, prefix = "") result = {} exp_keys = expected.is_a?(Hash) ? expected.keys : (expected.is_a?(Array) ? (0...expected.size) : nil) act_keys = actual.is_a?(Hash) ? actual.keys : (actual.is_a?(Array) ? (0...actual.size) : nil) if expected.is_a?(Hash) && actual.is_a?(Hash) all_keys = (expected.keys + actual.keys).uniq all_keys.each do |k| path = prefix.empty? ? k.to_s : "#{prefix}.#{k}" exp_v = expected[k] act_v = actual[k] if !expected.key?(k) result[path] = { expected: nil, got: act_v } elsif !actual.key?(k) result[path] = { expected: exp_v, got: nil } elsif exp_v.is_a?(Hash) && act_v.is_a?(Hash) result.merge!(diff(exp_v, act_v, path)) elsif exp_v.is_a?(Array) && act_v.is_a?(Array) max_len = [exp_v.size, act_v.size].max max_len.times do |i| result.merge!(diff(exp_v[i], act_v[i], "#{path}[#{i}]")) end elsif exp_v != act_v result[path] = { expected: exp_v, got: act_v } end end elsif expected.is_a?(Array) && actual.is_a?(Array) max_len = [expected.size, actual.size].max max_len.times do |i| result.merge!(diff(expected[i], actual[i], prefix.empty? ? "[#{i}]" : "#{prefix}[#{i}]")) end elsif expected != actual result[prefix] = { expected: expected, got: actual } unless prefix.empty? end result end |
.flatten_keys(hash, prefix = "") ⇒ Hash
Flattens a nested hash so keys become dot-notation strings.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nice/hash/flatten_keys.rb', line 16 def self.flatten_keys(hash, prefix = "") return {} unless hash.is_a?(Hash) result = {} hash.each do |k, v| path = prefix.empty? ? k.to_s : "#{prefix}.#{k}" if v.is_a?(Hash) result.merge!(flatten_keys(v, path)) else result[path] = v end end result end |
.get_all_keys(h) ⇒ Object
Get all the keys of a hash
@param hash [Hash] The hash
@return [Array]
@example my_hash = { uno: {tres: 3} } NiceHash.get_all_keys(my_hash) #>[:uno, :dos, :tres]
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nice/hash/get_all_keys.rb', line 14 def self.get_all_keys(h) h.each_with_object([]) do |(k, v), keys| keys << k keys.concat(get_all_keys(v)) if v.is_a? Hash if v.is_a?(Array) v.each do |vv| keys.concat(get_all_keys(vv)) if vv.is_a? Hash or vv.is_a? Array end end end end |
.nice_filter(hash, keys) ⇒ Object
Filter the hash supplied and returns only the specified keys
@param hash [Hash] The hash we want to filter @param keys [Array] [Symbol] Array of symbols or symbol. Nested keys can be used: 'uno.dos.tres'
@return [Hash]
@example my_hash = { user: { address: { city: 'Madrid', country: 'Spain' }, name: 'Peter', age: 33, customers: ['Peter', currency: 'Euro', currency: 'Euro'] }, customer: true } NiceHash.nice_filter(my_hash, [:'user.address.city', :'customer', :'user.customers.name']) #> => {:address => {:city => "Madrid", :customers => [=> "Peter", => "John"]}, :customer => true}
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/nice/hash/nice_filter.rb', line 26 def self.nice_filter(hash, keys) result = {} keys = [keys] unless keys.is_a?(Array) keys.each do |k| kn = k.to_s.split('.') if hash.is_a?(Hash) and hash.key?(k) if hash[k].is_a?(Hash) result[k] = {} unless result.key?(k) else result[k] = hash[k] end elsif hash.is_a?(Hash) and hash.key?(kn.first.to_sym) keys_nested = [] keys.grep(/^#{kn.first}\./).each do |k2| keys_nested << k2.to_s.gsub(/^#{kn.first}\./,'').to_sym end result[kn.first.to_sym] = nice_filter(hash[kn.first.to_sym], keys_nested) elsif hash.is_a?(Array) result = [] hash.each do |a| res = nice_filter(a, keys) result << res unless res.empty? end end end return result end |
.set_nested(hash, nested_key, value, only_if_exist = false) ⇒ Object
sets the supplied value on the supplied nested key
@param hash [Hash] The hash we want @param nested_key [Hash] [String] String with the nested key: 'uno.dos.tres' or a hash { uno: :tres } @param value [] value to set
@return [Hash]
@example my_hash = { user: { address: { city: 'Madrid', country: 'Spain' }, name: 'Peter', age: 33 }, customer: true } NiceHash.set_nested(my_hash, 'user.address.city', 'Barcelona') #>:country=>"Spain", :name=>"Peter", :age=>33}, :customer=>true}
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/nice/hash/set_nested.rb', line 25 def self.set_nested(hash, nested_key, value, only_if_exist = false) nested_key = transtring(nested_key) keys = nested_key.split(".") if keys.size == 1 hash[nested_key.to_sym] = value unless only_if_exist and !hash.key?(nested_key.to_sym) else parent = hash keys[0..-2].each do |k| sym = k.to_sym return hash unless parent.is_a?(Hash) && parent.key?(sym) parent = parent[sym] end return hash unless parent.is_a?(Hash) if only_if_exist return hash unless parent.key?(keys[-1].to_sym) end parent[keys[-1].to_sym] = value end return hash end |
.transtring(hash) ⇒ Object
Translate a hash of hashes into a string separted by .
@param hash [Hash] The hash we want to translate
@return [String]
@example my_hash = { uno: :tres } NiceHash.transtring(my_hash) #>"uno.dos.tres"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/nice/hash/transtring.rb', line 14 def self.transtring(hash) keys = [] if hash.is_a?(Hash) hash.each do |k, v| if v.is_a?(Hash) keys << k keys << transtring(v) else keys << k keys << v end end else keys << hash end return keys.join(".") end |
.unflatten_keys(hash) ⇒ Hash
Unflattens a hash with dot-notation keys into a nested hash.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/nice/hash/flatten_keys.rb', line 41 def self.unflatten_keys(hash) return {} unless hash.is_a?(Hash) result = {} hash.each do |key_str, value| keys = key_str.to_s.split(".") keys = keys.map { |k| k.match?(/\A\d+\z/) ? k.to_i : k.to_sym } current = result keys[0..-2].each do |k| current[k] = {} unless current[k].is_a?(Hash) current = current[k] end current[keys[-1]] = value end result end |