Class: DotHash::Properties

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

Direct Known Subclasses

Settings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Properties



6
7
8
# File 'lib/dot_hash/properties.rb', line 6

def initialize(hash)
  @hash = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/dot_hash/properties.rb', line 10

def method_missing(key, *args, &block)
  if has_key?(key)
    execute(key, *args, &block)
  else
    super(key, *args, &block)
  end
end

Instance Attribute Details

#hashObject (readonly) Also known as: to_hash

Returns the value of attribute hash.



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

def hash
  @hash
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/dot_hash/properties.rb', line 22

def ==(other)
  super(other) or other.to_hash == hash
end

#[](key) ⇒ Object



34
35
36
# File 'lib/dot_hash/properties.rb', line 34

def [](key)
  fetch(key, nil)
end

#fetch(key, *args, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/dot_hash/properties.rb', line 38

def fetch(key, *args, &block)
  key = hash.has_key?(key.to_s) ? key.to_s : key.to_sym
  value = hash.fetch(key, *args, &block)

  return value unless value.is_a?(Hash)
  hash[key] = self.class.new value
end

#has_key?(key) ⇒ Boolean



46
47
48
49
50
# File 'lib/dot_hash/properties.rb', line 46

def has_key?(key)
  hash.has_key?(key.to_s) or
    hash.has_key?(key.to_sym) or
    hash.respond_to?(key)
end

#respond_to_missing?(key, *args) ⇒ Boolean



18
19
20
# File 'lib/dot_hash/properties.rb', line 18

def respond_to_missing?(key, *args)
  has_key?(key) or super(key, *args)
end

#to_jsonObject



30
31
32
# File 'lib/dot_hash/properties.rb', line 30

def to_json
  hash.to_json
end

#to_sObject



26
27
28
# File 'lib/dot_hash/properties.rb', line 26

def to_s
  hash.to_s
end