Class: Smartgen::ObjectHash

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

Overview

A hash that has method accessors for each key.

For example:

hash = ObjectHash.new({:foo => 'bar'})
puts hash.foo     # outputs 'bar'

Direct Known Subclasses

Configuration

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)



24
25
26
27
28
29
30
31
32
33
# File 'lib/smartgen/object_hash.rb', line 24

def method_missing(name, *args)
  if has_key?(name)
    self[name]
  elsif setter?(name)
    self[name.to_s.chop] = args.first
  else
    $stderr.puts "warning: key #{name} not found on #{inspect}"
    Smartgen::ObjectHash.new
  end
end

Instance Method Details

#dupObject



11
12
13
# File 'lib/smartgen/object_hash.rb', line 11

def dup
  Smartgen::ObjectHash.new(self)
end

#inspectObject



19
20
21
# File 'lib/smartgen/object_hash.rb', line 19

def inspect
  "ObjectHash(#{super})"
end

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

Returns:

  • (Boolean)


15
16
17
# File 'lib/smartgen/object_hash.rb', line 15

def respond_to?(method, include_all = false)
  has_key?(method) || (setter?(method) && has_key?(method.to_s.chop)) || super
end