Class: Lol::DynamicModel

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/lol/dynamic_model.rb

Overview

DynamicModel extends OpenStruct adding the following features:

  • nested generation ({}) results in DynamicModel(a: DynamicModel)

  • parsing of date/time when property name ends with _at or _date and the value is a number

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ DynamicModel

Returns a new instance of DynamicModel.

Raises:

  • (ArgumentError)

11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lol/dynamic_model.rb', line 11

def initialize(hash={})
  raise ArgumentError, 'An hash is required as parameter' unless hash.is_a? Hash
  @raw = hash
  @table = {}
  @hash_table = {}

  hash.each do |k,v|
    key = k.to_s.underscore
    set_property key, v
    new_ostruct_member(key)
  end
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw


9
10
11
# File 'lib/lol/dynamic_model.rb', line 9

def raw
  @raw
end

Instance Method Details

#as_json(opts = {}) ⇒ Object


28
29
30
# File 'lib/lol/dynamic_model.rb', line 28

def as_json opts={}
  @table.as_json
end

#to_hObject


24
25
26
# File 'lib/lol/dynamic_model.rb', line 24

def to_h
  @hash_table
end