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

Returns a new instance of Properties.



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

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



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

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, to_h

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



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

def ==(other)
  super(other) ||
    (other.respond_to?(:to_hash) && other.to_hash == hash)
end

#[](key) ⇒ Object



36
37
38
# File 'lib/dot_hash/properties.rb', line 36

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

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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dot_hash/properties.rb', line 40

def fetch(key, *args, &block)
  key_sym = key.to_sym

  value = cached.fetch(key_sym) do
    key = hash.has_key?(key_sym) ? key_sym : key.to_s
    hashify_item hash.fetch(key, *args, &block)
  end

  return value if skip_cache?(key_sym, value)
  cached[key_sym] = value
end

#has_key?(key) ⇒ Boolean

Returns:



52
53
54
55
56
# File 'lib/dot_hash/properties.rb', line 52

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

Returns:



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

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

#to_jsonObject



32
33
34
# File 'lib/dot_hash/properties.rb', line 32

def to_json
  hash.to_json
end

#to_sObject



28
29
30
# File 'lib/dot_hash/properties.rb', line 28

def to_s
  hash.to_s
end