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.



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

def deep
  @deep
end

Instance Method Details

#[](k) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/lydown/core_ext.rb', line 56

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



67
68
69
70
71
72
73
74
75
# File 'lib/lydown/core_ext.rb', line 67

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



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

def deep!
  @deep = true
  self
end

#deep_cloneObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lydown/core_ext.rb', line 27

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.



8
9
10
11
# File 'lib/lydown/core_ext.rb', line 8

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

#deep_merge!(hash) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lydown/core_ext.rb', line 13

def deep_merge!(hash)
  hash.keys.each do |key|
    if hash[key].is_a? Hash and self[key].is_a? Hash
      self[key] = self[key].deep_merge!(hash[key])
      next
    end

    self[key] = hash[key]
  end

  self.deep = true
  self
end

#lookup(path) ⇒ Object



42
43
44
# File 'lib/lydown/core_ext.rb', line 42

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

#merge(hash) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/lydown/core_ext.rb', line 78

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

#merge!(hash) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/lydown/core_ext.rb', line 87

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

#old_getObject



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

alias_method :old_get, :[]

#old_mergeObject



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

alias_method :old_merge, :merge

#old_merge!Object



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

alias_method :old_merge!, :merge!

#old_setObject



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

alias_method :old_set, :[]=

#set(path, value) ⇒ Object



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

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