Class: EML::Model

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/eml/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_values) ⇒ Model

Returns a new instance of Model.



33
34
35
36
37
38
# File 'lib/eml/model.rb', line 33

def initialize(raw_values)
  self.class.enumerate_fields do |response_name, local_name|
    value = field_value(response_name, raw_values[response_name])
    instance_variable_set(:"@#{local_name}", value)
  end
end

Class Method Details

.enumerate_fieldsObject



25
26
27
28
29
30
# File 'lib/eml/model.rb', line 25

def self.enumerate_fields
  const_get(:FIELDS).each do |response_name, local_name|
    local_name ||= response_name.to_sym
    yield(response_name, local_name)
  end
end

.fields(fields) ⇒ Object



17
18
19
20
21
22
# File 'lib/eml/model.rb', line 17

def self.fields(fields)
  const_set(:FIELDS, fields.freeze)
  enumerate_fields do |_, local_name|
    __send__(:attr_reader, :"#{local_name}")
  end
end

Instance Method Details

#to_hObject



41
42
43
# File 'lib/eml/model.rb', line 41

def to_h
  ModelHash.(self)
end

#to_json(*_args) ⇒ Object



46
47
48
# File 'lib/eml/model.rb', line 46

def to_json(*_args)
  to_h.to_json
end