Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/lydown/core_ext.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deepObject

Returns the value of attribute deep.



59
60
61
# File 'lib/lydown/core_ext.rb', line 59

def deep
  @deep
end

Instance Method Details

#[](k) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/lydown/core_ext.rb', line 62

def [](k)
  if @deep && k.is_a?(String) && k =~ /\//
    lookup(k)
  elsif @deep && k.is_a?(Symbol)
    old_get(k) || old_get(k.to_s)      
  else
    old_get(k)
  end
end

#[]=(k, v) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/lydown/core_ext.rb', line 73

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



101
102
103
104
# File 'lib/lydown/core_ext.rb', line 101

def deep!
  @deep = true
  self
end

#deep_cloneObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lydown/core_ext.rb', line 33

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.



11
12
13
14
# File 'lib/lydown/core_ext.rb', line 11

def deep_merge(hash)
  target = Marshal.load(Marshal.dump(self))
  target.deep_merge!(hash)
end

#deep_merge!(hash) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lydown/core_ext.rb', line 16

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/lydown/core_ext.rb', line 106

def is_deep?
  !!@deep
end

#lookup(path) ⇒ Object



48
49
50
# File 'lib/lydown/core_ext.rb', line 48

def lookup(path)
  path.split("/").inject(self) {|m,i| m[i].nil? ? (return nil) : m[i]}
end

#merge(hash) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/lydown/core_ext.rb', line 84

def merge(hash)
  if @deep || hash.deep
    deep_merge(hash)
  else
    old_merge(hash)
  end
end

#merge!(hash) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/lydown/core_ext.rb', line 93

def merge!(hash)
  if @deep || hash.deep
    deep_merge!(hash)
  else
    old_merge!(hash)
  end
end

#old_getObject



61
# File 'lib/lydown/core_ext.rb', line 61

alias_method :old_get, :[]

#old_mergeObject



83
# File 'lib/lydown/core_ext.rb', line 83

alias_method :old_merge, :merge

#old_merge!Object



92
# File 'lib/lydown/core_ext.rb', line 92

alias_method :old_merge!, :merge!

#old_setObject



72
# File 'lib/lydown/core_ext.rb', line 72

alias_method :old_set, :[]=

#set(path, value) ⇒ Object



52
53
54
55
56
57
# File 'lib/lydown/core_ext.rb', line 52

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

#stringify_keysObject



110
111
112
113
114
# File 'lib/lydown/core_ext.rb', line 110

def stringify_keys
  h = {}
  each {|k, v| h[k.to_s] = v}
  h
end