Class: FrenchMan::ObjectifiedHash

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ObjectifiedHash

:nodoc:



105
106
107
# File 'lib/french_man.rb', line 105

def initialize(attributes = {}) #:nodoc:
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

:nodoc:



125
126
127
128
129
130
131
# File 'lib/french_man.rb', line 125

def method_missing(name, *args) #:nodoc:
  if @attributes.has_key? name
    value = name.is_a?(Hash) ? self.class.new(@attributes[name]) : @attributes[name]
  else
    @attributes.send name, *args
  end
end

Instance Method Details

#to_hashObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/french_man.rb', line 109

def to_hash
  hash = {}
  @attributes.each do |key, val|
    if val.is_a? Array
      hash[key] = val.map { |elmt| (elmt.respond_to? 'to_hash') ? elmt.to_hash : elmt }
    else
      hash[key] = (val.respond_to? 'to_hash') ? val.to_hash : val
    end
  end
  hash
end

#to_json(options = {}) ⇒ Object

by default Object.to_json return ‘attributes’ root key



121
122
123
# File 'lib/french_man.rb', line 121

def to_json(options={}) # by default Object.to_json return 'attributes' root key
  to_hash.to_json(options)
end