Module: Hammock::HashPatches::InstanceMethods
- Defined in:
- lib/hammock/monkey_patches/hash.rb
Instance Method Summary collapse
- #discard(*keys) ⇒ Object
- #discard!(*keys) ⇒ Object
-
#dragnet(*keys) ⇒ Object
TODO remove in favour of #slice (in ActiveSupport).
- #dragnet!(*keys) ⇒ Object
- #selekt(&block) ⇒ Object
- #to_flattened_json ⇒ Object
- #to_param_hash(prefix = '') ⇒ Object
Instance Method Details
#discard(*keys) ⇒ Object
20 21 22 |
# File 'lib/hammock/monkey_patches/hash.rb', line 20 def discard *keys dup.discard! *keys end |
#discard!(*keys) ⇒ Object
15 16 17 18 |
# File 'lib/hammock/monkey_patches/hash.rb', line 15 def discard! *keys keys.each {|k| delete k } self end |
#dragnet(*keys) ⇒ Object
TODO remove in favour of #slice (in ActiveSupport).
33 34 35 |
# File 'lib/hammock/monkey_patches/hash.rb', line 33 def dragnet *keys dup.dragnet! *keys end |
#dragnet!(*keys) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/hammock/monkey_patches/hash.rb', line 37 def dragnet! *keys keys.inject({}) {|acc,key| acc[key] = self.delete(key) if self.has_key?(key) acc } end |
#selekt(&block) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/hammock/monkey_patches/hash.rb', line 24 def selekt &block hsh = {} each_pair {|k,v| hsh[k] = v if yield(k,v) } hsh end |
#to_flattened_json ⇒ Object
52 53 54 |
# File 'lib/hammock/monkey_patches/hash.rb', line 52 def to_flattened_json to_param_hash.to_json end |
#to_param_hash(prefix = '') ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/hammock/monkey_patches/hash.rb', line 44 def to_param_hash prefix = '' hsh = self.dup # TODO these two blocks can probably be combined hsh.keys.each {|k| hsh.merge!(hsh.delete(k).to_param_hash(k)) if hsh[k].is_a?(Hash) } hsh.keys.each {|k| hsh["#{prefix}[#{k}]"] = hsh.delete(k) } unless prefix.blank? hsh end |