Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/beef/core_ext/hash/deep_merge.rb,
lib/beef/core_ext/hash/method_access.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/beef/core_ext/hash/method_access.rb', line 10

def method_missing(name, *args)
  return self[name.to_s] if key?(name.to_s)
  return self[name.to_sym] if key?(name.to_sym)
  if args.size == 1 && name.to_s =~ /(.*)=$/
    return self[$1.to_s] = args.first
  end
  if args.empty? && name.to_s =~ /(.*)\?$/
    if (key?($1) || key?($1.to_sym))
      return self[$1] || self[$1.to_sym]
    else
      return false
    end
  end
  super
end

Instance Method Details

#deep_merge(other_hash) ⇒ Object



3
4
5
# File 'lib/beef/core_ext/hash/deep_merge.rb', line 3

def deep_merge(other_hash)
  dup.deep_merge!(other_hash)
end

#deep_merge!(other_hash) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/beef/core_ext/hash/deep_merge.rb', line 7

def deep_merge!(other_hash)
  other_hash.each do |k,v|
    tv = self[k]
    self[k] = tv.is_a?(::Hash) && v.is_a?(::Hash) ? tv.deep_merge(v) : v
  end
  self
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
# File 'lib/beef/core_ext/hash/method_access.rb', line 3

def respond_to?(name, include_private = false)
  return true if key?(name.to_s) || key?(name.to_sym)
  return true if name.to_s =~ /=$/
  return true if name.to_s =~ /(.*)\?$/ && (key?($1) || key?($1.to_sym))
  super
end