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 Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ DynamicModel

Returns a new instance of DynamicModel.

Raises:

  • (ArgumentError)


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

def initialize(hash={})
  raise ArgumentError, 'An hash is required as parameter' unless hash.is_a? 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 Method Details

#as_json(opts = {}) ⇒ Object



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

def as_json opts={}
  @table.as_json
end

#to_hObject



21
22
23
# File 'lib/lol/dynamic_model.rb', line 21

def to_h
  @hash_table
end