Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/lydown/core_ext.rb
Instance Attribute Summary collapse
-
#deep ⇒ Object
Returns the value of attribute deep.
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #deep! ⇒ Object
- #deep_clone ⇒ Object
-
#deep_merge(hash) ⇒ Object
Merges self with another hash, recursively.
- #deep_merge!(hash) ⇒ Object
- #is_deep? ⇒ Boolean
- #lookup(path) ⇒ Object
- #merge(hash) ⇒ Object
- #merge!(hash) ⇒ Object
- #old_get ⇒ Object
- #old_merge ⇒ Object
- #old_merge! ⇒ Object
- #old_set ⇒ Object
- #set(path, value) ⇒ Object
Instance Attribute Details
#deep ⇒ Object
Returns the value of attribute deep.
58 59 60 |
# File 'lib/lydown/core_ext.rb', line 58 def deep @deep end |
Instance Method Details
#[](k) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/lydown/core_ext.rb', line 61 def [](k) if @deep && k.is_a?(String) && k =~ /\// lookup(k) elsif @deep && k.is_a?(Symbol) old_get(k.to_s) else old_get(k) end end |
#[]=(k, v) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/lydown/core_ext.rb', line 72 def []=(k, v) if @deep && k.is_a?(String) && k =~ /\// set(k, v) elsif @deep && k.is_a?(Symbol) old_set(k.to_s, v) else old_set(k, v) end end |
#deep! ⇒ Object
100 101 102 103 |
# File 'lib/lydown/core_ext.rb', line 100 def deep! @deep = true self end |
#deep_clone ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lydown/core_ext.rb', line 32 def deep_clone dest = {}.deep! each do |k, v| dest[k] = case v when Hash v.deep_clone when Array v.clone else v end end dest end |
#deep_merge(hash) ⇒ Object
Merges self with another hash, recursively.
This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html
Thanks to whoever made it.
10 11 12 13 |
# File 'lib/lydown/core_ext.rb', line 10 def deep_merge(hash) target = Marshal.load(Marshal.dump(self)) target.deep_merge!(hash) end |
#deep_merge!(hash) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lydown/core_ext.rb', line 15 def deep_merge!(hash) unless self.hash == hash.hash hash.each do |key, value| svalue = self.old_get(key) if (value.is_a? Hash) && (svalue.is_a? Hash) old_set(key, svalue.deep_merge(value)) else old_set(key, value) end end end self.deep = true self end |
#is_deep? ⇒ Boolean
105 106 107 |
# File 'lib/lydown/core_ext.rb', line 105 def is_deep? !!@deep end |
#lookup(path) ⇒ Object
47 48 49 |
# File 'lib/lydown/core_ext.rb', line 47 def lookup(path) path.split("/").inject(self) {|m,i| m[i].nil? ? (return nil) : m[i]} end |
#merge(hash) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/lydown/core_ext.rb', line 83 def merge(hash) if @deep || hash.deep deep_merge(hash) else old_merge(hash) end end |
#merge!(hash) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/lydown/core_ext.rb', line 92 def merge!(hash) if @deep || hash.deep deep_merge!(hash) else old_merge!(hash) end end |
#old_get ⇒ Object
60 |
# File 'lib/lydown/core_ext.rb', line 60 alias_method :old_get, :[] |
#old_merge ⇒ Object
82 |
# File 'lib/lydown/core_ext.rb', line 82 alias_method :old_merge, :merge |
#old_merge! ⇒ Object
91 |
# File 'lib/lydown/core_ext.rb', line 91 alias_method :old_merge!, :merge! |
#old_set ⇒ Object
71 |
# File 'lib/lydown/core_ext.rb', line 71 alias_method :old_set, :[]= |
#set(path, value) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/lydown/core_ext.rb', line 51 def set(path, value) leafs = path.split("/") k = leafs.pop h = leafs.inject(self) {|m, i| m[i].is_a?(Hash) ? m[i] : (m[i] = {})} h[k] = value end |