Class: Loquor::ObjectHash

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

Instance Method Summary collapse

Constructor Details

#initialize(hash, options = {}) ⇒ ObjectHash

Returns a new instance of ObjectHash.



6
7
8
9
# File 'lib/loquor/object_hash.rb', line 6

def initialize(hash, options = {})
  @hash = hash
  @strict = options[:strict]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/loquor/object_hash.rb', line 31

def method_missing(name, *args)
  if name[-1] == "="
    @hash.send(name, *args) 
  else
    self[name]
  end
end

Instance Method Details

#==(other) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/loquor/object_hash.rb', line 11

def ==(other)
  if other.is_a?(ObjectHash)
    @hash == other.get_instance_variable(:@hash)
  elsif other.is_a?(Hash)
    @hash == other
  else
    false
  end
end

#[](key) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/loquor/object_hash.rb', line 21

def [](key)
  fetch_indescriminately(key)
rescue ObjectHashKeyMissingError
  if @strict
    raise $!
  else
    nil
  end
end

#respond_to?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/loquor/object_hash.rb', line 39

def respond_to?(key)
  return true if super
  @hash.has_key?(key.to_s) || @hash.has_key?(key.to_sym)
end