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
- #reject_r(&block) ⇒ Object
- #reject_r!(&block) ⇒ 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).
32 33 34 |
# File 'lib/hammock/monkey_patches/hash.rb', line 32 def dragnet *keys dup.dragnet! *keys end |
#dragnet!(*keys) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/hammock/monkey_patches/hash.rb', line 36 def dragnet! *keys keys.inject({}) {|acc,key| acc[key] = self.delete(key) if self.has_key?(key) acc } end |
#reject_r(&block) ⇒ Object
55 56 57 |
# File 'lib/hammock/monkey_patches/hash.rb', line 55 def reject_r &block dup.reject_r! &block end |
#reject_r!(&block) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/hammock/monkey_patches/hash.rb', line 59 def reject_r! &block each_pair {|k,v| if yield k, v self.delete k elsif v.is_a? Hash self[k] = v.reject_r &block end } end |
#selekt(&block) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/hammock/monkey_patches/hash.rb', line 24 def selekt &block inject({}) {|hsh,(k,v)| hsh[k] = v if yield(k,v) hsh } end |
#to_flattened_json ⇒ Object
51 52 53 |
# File 'lib/hammock/monkey_patches/hash.rb', line 51 def to_flattened_json to_param_hash.to_json end |
#to_param_hash(prefix = '') ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/hammock/monkey_patches/hash.rb', line 43 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 |