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
Returns a new instance of BaseModel.
45
46
47
48
49
50
51
52
|
# File 'lib/ecoportal/api/common/base_model.rb', line 45
def initialize(doc = {}, parent: self, key: nil)
@_parent = parent
@_key = key
if !_parent || !_key
@doc = doc
@original_doc = JSON.parse(@doc.to_json)
end
end
|
Instance Attribute Details
#_key ⇒ Object
Returns the value of attribute _key.
43
44
45
|
# File 'lib/ecoportal/api/common/base_model.rb', line 43
def _key
@_key
end
|
#_parent ⇒ Object
Returns the value of attribute _parent.
43
44
45
|
# File 'lib/ecoportal/api/common/base_model.rb', line 43
def _parent
@_parent
end
|
Class Method Details
.embeds_one(method, key: method, nullable: false, klass:) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/ecoportal/api/common/base_model.rb', line 26
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
return instance_variable_get(var) if instance_variable_defined?(var)
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
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/ecoportal/api/common/base_model.rb', line 14
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
66
67
68
|
# File 'lib/ecoportal/api/common/base_model.rb', line 66
def as_json
doc
end
|
#as_update ⇒ Object
74
75
76
77
|
# File 'lib/ecoportal/api/common/base_model.rb', line 74
def as_update
new_doc = as_json
Common::HashDiff.diff(new_doc, original_doc)
end
|
#consolidate! ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/ecoportal/api/common/base_model.rb', line 83
def consolidate!
raise UnlinkedModel.new unless linked?
case
when @original_doc
@original_doc = JSON.parse(@doc.to_json)
else
_parent.original_doc[_key] = JSON.parse(doc.to_json)
end
end
|
#dirty? ⇒ Boolean
79
80
81
|
# File 'lib/ecoportal/api/common/base_model.rb', line 79
def dirty?
as_update != {}
end
|
#doc ⇒ Object
54
55
56
57
58
|
# File 'lib/ecoportal/api/common/base_model.rb', line 54
def doc
raise UnlinkedModel.new unless linked?
return _parent.doc[_key] unless _parent == self
@doc
end
|
#original_doc ⇒ Object
60
61
62
63
64
|
# File 'lib/ecoportal/api/common/base_model.rb', line 60
def original_doc
raise UnlinkedModel.new unless linked?
return _parent.original_doc[_key] unless _parent == self
@original_doc
end
|
#print ⇒ Object
103
104
105
106
|
# File 'lib/ecoportal/api/common/base_model.rb', line 103
def print
puts JSON.pretty_generate(as_json)
self
end
|
#reset! ⇒ Object
93
94
95
96
97
98
99
100
101
|
# File 'lib/ecoportal/api/common/base_model.rb', line 93
def reset!
raise UnlinkedModel.new unless linked?
case
when @doc
@doc = JSON.parse(@original_doc.to_json)
else
_parent.doc[_key] = JSON.parse(original_doc.to_json)
end
end
|
#to_json(*args) ⇒ Object
70
71
72
|
# File 'lib/ecoportal/api/common/base_model.rb', line 70
def to_json(*args)
doc.to_json(*args)
end
|