Class: Economic::Model

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Model

Returns a new instance of Model.

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
# File 'lib/economic/model.rb', line 66

def initialize(**kwargs)
  valid_keys = self.class.attributes.map(&:name) + (self.class.relations&.map(&:name) || [])
  invalid_keys = kwargs.keys - valid_keys
  raise ArgumentError, "invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?

  self.class.attributes.each { |attribute| instance_variable_set :"@#{attribute.name}", kwargs[attribute.name] }
  self.class.relations&.each { |relation| instance_variable_set :"@#{relation.name}", relation_value(relation, kwargs) }
end

Class Method Details

.attributesObject



57
58
59
# File 'lib/economic/model.rb', line 57

def attributes
  @attributes ||= []
end

.field(name, as: nil) ⇒ Object



4
5
6
7
# File 'lib/economic/model.rb', line 4

def field(name, as: nil)
  (@attributes ||= []) << Attribute.new(name:, as:)
  attr_accessor name
end

.from_hash(hash) ⇒ Object



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

def from_hash(hash)
  translated_attributes = translate_attributes(hash)
  translated_relations = translate_relations(hash, translated_attributes)
  new(**translated_relations)
end

.from_json(json) ⇒ Object



14
15
16
# File 'lib/economic/model.rb', line 14

def from_json(json)
  from_hash(JSON.parse(json))
end

.inherited(subclass) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/economic/model.rb', line 46

def inherited(subclass)
  return if self == Economic::Model

  attributes.each do |attribute|
    subclass.attributes << attribute
  end
  relations.each do |relation|
    subclass.relations << relation
  end
end

.relation(name, as: nil, multiple: false, klass: nil) ⇒ Object



9
10
11
12
# File 'lib/economic/model.rb', line 9

def relation(name, as: nil, multiple: false, klass: nil)
  (@relations ||= []) << Relation.new(name:, as:, multiple:, klass:)
  attr_accessor name
end

.relationsObject



61
62
63
# File 'lib/economic/model.rb', line 61

def relations
  @relations ||= []
end

.translate_attributes(hash) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/economic/model.rb', line 24

def translate_attributes(hash)
  economic_attribute_names = attributes.map(&:economic_name)
  attributes_hash = hash.slice(*economic_attribute_names)

  attributes_hash.each_with_object({}) do |(key, value), result|
    attribute = attributes.find { |attr| attr.as.to_s == key || attr.name.to_s.camelize(:lower) == key }
    result[attribute.name] = value
  end
end

.translate_relations(hash, translated_attributes) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/economic/model.rb', line 34

def translate_relations(hash, translated_attributes)
  return translated_attributes unless relations&.any?

  economic_relation_names = relations.map(&:economic_name)
  relations_hash = hash.slice(*economic_relation_names)

  relations_hash.each_with_object(translated_attributes) do |(key, value), result|
    relation = relations.find { |rel| rel.as.to_s == key || rel.name.to_s.camelize(:lower) == key }
    result[relation.name] = relation.multiple? ? value.map { relation.klass.from_hash(_1) } : relation.klass.from_hash(value)
  end
end

Instance Method Details

#to_hObject



75
76
77
78
79
80
# File 'lib/economic/model.rb', line 75

def to_h
  result = {}
  self.class.attributes.each_with_object(result) { |attribute, hash| hash[attribute.economic_name] = instance_variable_get :"@#{attribute.name}" }
  self.class.relations&.each_with_object(result) { |relation, hash| hash[relation.economic_name] = relation_to_h(relation) }
  result.compact
end

#to_jsonObject



82
83
84
# File 'lib/economic/model.rb', line 82

def to_json
  to_h.to_json
end