Class: Hash
- Defined in:
- lib/wedge/utilis/hash.rb,
lib/wedge/utilis/blank.rb
Direct Known Subclasses
Instance Method Summary collapse
- #deep_merge(second) ⇒ Object
-
#extract!(*keys) ⇒ Object
Removes and returns the key/value pairs matching the given keys.
- #indifferent ⇒ Object
-
#slice(*keys) ⇒ Object
the given keys.
-
#slice!(*keys) ⇒ Object
Replaces the hash with only the given keys.
-
#to_obj ⇒ Object
add keys to hash.
Instance Method Details
#deep_merge(second) ⇒ Object
23 24 25 26 |
# File 'lib/wedge/utilis/hash.rb', line 23 def deep_merge(second) merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } self.merge(second, &merger) end |
#extract!(*keys) ⇒ Object
Removes and returns the key/value pairs matching the given keys.
{ a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
{ a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
71 72 73 |
# File 'lib/wedge/utilis/hash.rb', line 71 def extract!(*keys) keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) } end |
#indifferent ⇒ Object
75 76 77 |
# File 'lib/wedge/utilis/hash.rb', line 75 def indifferent Wedge::IndifferentHash.new self end |
#slice(*keys) ⇒ Object
the given keys.
{ a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
# => {:a=>1, :b=>2}
This is useful for limiting an options hash to valid keys before passing to a method:
def search(criteria = {})
criteria.assert_valid_keys(:mass, :velocity, :time)
end
search(.slice(:mass, :velocity, :time))
If you have an array of keys you want to limit to, you should splat them:
valid_keys = [:mass, :velocity, :time]
search(.slice(*valid_keys))
47 48 49 50 |
# File 'lib/wedge/utilis/hash.rb', line 47 def slice(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) } end |
#slice!(*keys) ⇒ Object
Replaces the hash with only the given keys. Returns a hash containing the removed key/value pairs.
{ a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
# => {:c=>3, :d=>4}
57 58 59 60 61 62 63 64 65 |
# File 'lib/wedge/utilis/hash.rb', line 57 def slice!(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end |
#to_obj ⇒ Object
add keys to hash
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/wedge/utilis/hash.rb', line 3 def to_obj self.each do |k,v| if v.kind_of? Hash v.to_obj end k=k.to_s.gsub(/\.|\s|-|\/|\'/, '_').downcase.to_sym ## create and initialize an instance variable for this key/value pair self.instance_variable_set("@#{k}", v) ## create the getter that returns the instance variable self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the setter that sets the instance variable self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) end return self end |