Module: AILabTools::ResponseModel::ClassMethods

Defined in:
lib/ailabtools/response_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#field_mapObject (readonly)

Returns the value of attribute field_map.



10
11
12
# File 'lib/ailabtools/response_model.rb', line 10

def field_map
  @field_map
end

#field_typesObject (readonly)

Returns the value of attribute field_types.



10
11
12
# File 'lib/ailabtools/response_model.rb', line 10

def field_types
  @field_types
end

Instance Method Details

#fields(map, types = {}) ⇒ Object



12
13
14
15
16
# File 'lib/ailabtools/response_model.rb', line 12

def fields(map, types = {})
  @field_map = map
  @field_types = types
  attr_reader(*map.keys)
end

#from_hash(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/ailabtools/response_model.rb', line 18

def from_hash(hash)
  object = new
  object.instance_variable_set(:@raw, hash)
  (@field_map || {}).each do |attr, wire|
    value = hash[wire]
    value = convert_value(value, (@field_types || {})[attr])
    object.instance_variable_set("@#{attr}", value)
  end
  object
end