Class: Ecoportal::API::Common::BaseModel
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::BaseModel
show all
- Extended by:
- BaseClass
- Defined in:
- lib/ecoportal/api/common/base_model.rb
Defined Under Namespace
Classes: UnlinkedModel
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from BaseClass
class_resolver, resolve_class
Constructor Details
#initialize(doc = {}, parent: self, key: nil) ⇒ BaseModel
52
53
54
55
56
57
58
59
60
|
# File 'lib/ecoportal/api/common/base_model.rb', line 52
def initialize(doc = {}, parent: self, key: nil)
@_parent = parent
@_key = key
if !_parent || !_key
@doc = doc
@original_doc = JSON.parse(@doc.to_json)
@initial_doc = JSON.parse(@doc.to_json)
end
end
|
Instance Attribute Details
#_key ⇒ Object
Returns the value of attribute _key.
50
51
52
|
# File 'lib/ecoportal/api/common/base_model.rb', line 50
def _key
@_key
end
|
#_parent ⇒ Object
Returns the value of attribute _parent.
50
51
52
|
# File 'lib/ecoportal/api/common/base_model.rb', line 50
def _parent
@_parent
end
|
Class Method Details
.embeds_one(method, key: method, nullable: false, klass:) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/ecoportal/api/common/base_model.rb', line 28
def embeds_one(method, key: method, nullable: false, klass:)
method = method.to_s.freeze
var = "@#{method}".freeze
key = key.to_s.freeze
define_method(method) do
if instance_variable_defined?(var)
value = instance_variable_get(var)
return value unless nullable
return value if (value && doc[key]) || (!value && !doc[key])
remove_instance_variable(var)
end
doc[key] ||= {} unless nullable
return instance_variable_set(var, nil) unless doc[key]
self.class.resolve_class(klass).new(
doc[key], parent: self, key: key
).tap {|obj| instance_variable_set(var, obj)}
end
end
|
.passthrough(*methods, to: :doc) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/ecoportal/api/common/base_model.rb', line 16
def passthrough(*methods, to: :doc)
methods.each do |method|
method = method.to_s
define_method method do
send(to)[method]
end
define_method "#{method}=" do |value|
send(to)[method] = value
end
end
end
|
Instance Method Details
#as_json ⇒ Object
80
81
82
|
# File 'lib/ecoportal/api/common/base_model.rb', line 80
def as_json
doc
end
|
#as_update(ref = :last, ignore: []) ⇒ Object
88
89
90
91
92
|
# File 'lib/ecoportal/api/common/base_model.rb', line 88
def as_update(ref = :last, ignore: [])
new_doc = as_json
ref_doc = ref == :total ? initial_doc : original_doc
Common::HashDiff.diff(new_doc, ref_doc, ignore: ignore)
end
|
#consolidate! ⇒ Object
It consolidates all the changes carried by doc by setting it as original_doc.
99
100
101
102
103
104
105
106
107
|
# File 'lib/ecoportal/api/common/base_model.rb', line 99
def consolidate!
raise UnlinkedModel.new(from: "#{self.class}#consolidate!", key: _key) unless linked?
new_doc = JSON.parse(doc.to_json)
if is_root?
@original_doc = new_doc
else
dig_set(_parent.original_doc, [_key].flatten, new_doc)
end
end
|
#dirty? ⇒ Boolean
94
95
96
|
# File 'lib/ecoportal/api/common/base_model.rb', line 94
def dirty?
as_update != {}
end
|
#doc ⇒ Object
62
63
64
65
66
|
# File 'lib/ecoportal/api/common/base_model.rb', line 62
def doc
raise UnlinkedModel.new(from: "#{self.class}#doc", key: _key) unless linked?
return @doc if is_root?
_parent.doc.dig(*[_key].flatten)
end
|
#initial_doc ⇒ Object
74
75
76
77
78
|
# File 'lib/ecoportal/api/common/base_model.rb', line 74
def initial_doc
raise UnlinkedModel.new(from: "#{self.class}#initial_doc", key: _key) unless linked?
return @initial_doc if is_root?
_parent.initial_doc.dig(*[_key].flatten)
end
|
#original_doc ⇒ Object
68
69
70
71
72
|
# File 'lib/ecoportal/api/common/base_model.rb', line 68
def original_doc
raise UnlinkedModel.new(from: "#{self.class}#original_doc", key: _key) unless linked?
return @original_doc if is_root?
_parent.original_doc.dig(*[_key].flatten)
end
|
#print_pretty ⇒ Object
138
139
140
141
|
# File 'lib/ecoportal/api/common/base_model.rb', line 138
def print_pretty
puts JSON.pretty_generate(as_json)
self
end
|
#reset!(key = nil) ⇒ Object
Note:
- When there are nullable properties, it may be required to apply
reset! from the parent
i.e. parent.reset!("child") # when parent.child is nil
- In such a case, only immediate childs are allowed to be reset
It removes all the changes carried by doc by restoring original_doc into doc.
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/ecoportal/api/common/base_model.rb', line 115
def reset!(key = nil)
raise "'key' should be a String. Given #{key}" unless !key || key.is_a?(String)
raise UnlinkedModel.new(from: "#{self.class}#reset!", key: _key) unless linked?
if key
if self.respond_to?(key) && child = self.send(key) && child.is_a?(Ecoportal::API::Common::BaseModel)
child.reset!
else
new_doc = original_doc && original_doc[key]
dig_set(doc, [key], new_doc && JSON.parse(new_doc.to_json))
self.send(key) if !new_doc && self.respond_to?(key)
end
else
new_doc = JSON.parse(original_doc.to_json)
if is_root?
@doc = new_doc
else
dig_set(_parent.doc, [_key].flatten, new_doc)
end
end
end
|
#to_json(*args) ⇒ Object
84
85
86
|
# File 'lib/ecoportal/api/common/base_model.rb', line 84
def to_json(*args)
doc.to_json(*args)
end
|