Class: Hash

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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

def method_missing(method, *args)
  return super(method, *args) unless to_dot?

  method, chain = method.to_s.split('.', 2).map(&:to_sym)

  return send(method).send(chain, *args) if chain

  prop = create_prop(method)

  if setter?(method)
    self[prop] = args.first
  elsif key?(prop) || use_default?
    self[prop]
  else
    super(method, args)
  end
end

Class Attribute Details

.hash_dot_use_defaultObject

Returns the value of attribute hash_dot_use_default.



4
5
6
# File 'lib/hash.rb', line 4

def hash_dot_use_default
  @hash_dot_use_default
end

.use_dot_syntaxObject

Returns the value of attribute use_dot_syntax.



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

def use_dot_syntax
  @use_dot_syntax
end

Instance Attribute Details

#hash_dot_use_defaultObject

Returns the value of attribute hash_dot_use_default.



8
9
10
# File 'lib/hash.rb', line 8

def hash_dot_use_default
  @hash_dot_use_default
end

#use_dot_syntaxObject

Returns the value of attribute use_dot_syntax.



7
8
9
# File 'lib/hash.rb', line 7

def use_dot_syntax
  @use_dot_syntax
end

Instance Method Details

#respond_to?(method, include_all = false) ⇒ Boolean



34
35
36
37
38
39
# File 'lib/hash.rb', line 34

def respond_to?(method, include_all=false)
  return super(method, include_all) unless to_dot?
  prop = create_prop(method)
  return true if key?(prop)
  super(method, include_all)
end

#to_dot(use_default: false) ⇒ Object



10
11
12
13
14
# File 'lib/hash.rb', line 10

def to_dot(use_default: false)
  dotify_hash(self, use_default: use_default)

  self
end