Class: Ecoportal::API::Common::BaseModel
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::BaseModel
- Extended by:
- BaseClass
- Defined in:
- lib/ecoportal/api/common/base_model.rb
Direct Known Subclasses
Internal::Account, Internal::LoginProvider, Internal::Permissions, Internal::PolicyGroup, Internal::Preferences, V1::Person, V1::PersonDetails, V1::PersonSchema, V1::SchemaField, V1::SchemaFieldValue
Defined Under Namespace
Classes: UnlinkedModel
Instance Attribute Summary collapse
-
#_key ⇒ Object
readonly
Returns the value of attribute _key.
-
#_parent ⇒ Object
readonly
Returns the value of attribute _parent.
Class Method Summary collapse
- .embeds_one(method, klass:, key: method, nullable: false) ⇒ Object
- .passthrough(*methods, to: :doc) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
- #as_update(ref = :last, ignore: []) ⇒ Object
-
#consolidate! ⇒ Object
It consolidates all the changes carried by
doc
by setting it asoriginal_doc
. - #dirty? ⇒ Boolean
- #doc ⇒ Object
- #initial_doc ⇒ Object
-
#initialize(doc = {}, parent: self, key: nil) ⇒ BaseModel
constructor
A new instance of BaseModel.
- #original_doc ⇒ Object
- #print_pretty ⇒ Object
-
#replace_doc!(new_doc) ⇒ Hash
It replaces
doc
bynew_doc
. -
#replace_original_doc!(new_doc) ⇒ Hash
It replaces
original_doc
bynew_doc
. -
#reset!(key = nil) ⇒ Object
It removes all the changes carried by
doc
by restoringoriginal_doc
intodoc
. - #to_json(*args) ⇒ Object
Methods included from BaseClass
class_resolver, redef_without_warning, resolve_class
Constructor Details
#initialize(doc = {}, parent: self, key: nil) ⇒ BaseModel
Returns a new instance of BaseModel.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ecoportal/api/common/base_model.rb', line 55 def initialize(doc = {}, parent: self, key: nil) @_parent = parent @_key = key return unless !_parent || !_key @doc = JSON.parse(doc.to_json) @original_doc = JSON.parse(@doc.to_json) @initial_doc = JSON.parse(@doc.to_json) end |
Instance Attribute Details
#_key ⇒ Object (readonly)
Returns the value of attribute _key.
53 54 55 |
# File 'lib/ecoportal/api/common/base_model.rb', line 53 def _key @_key end |
#_parent ⇒ Object (readonly)
Returns the value of attribute _parent.
53 54 55 |
# File 'lib/ecoportal/api/common/base_model.rb', line 53 def _parent @_parent end |
Class Method Details
.embeds_one(method, klass:, key: method, nullable: false) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ecoportal/api/common/base_model.rb', line 29 def (method, klass:, key: method, nullable: false) 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
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ecoportal/api/common/base_model.rb', line 17 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
107 108 109 |
# File 'lib/ecoportal/api/common/base_model.rb', line 107 def as_json doc end |
#as_update(ref = :last, ignore: []) ⇒ Object
115 116 117 118 119 |
# File 'lib/ecoportal/api/common/base_model.rb', line 115 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
.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ecoportal/api/common/base_model.rb', line 126 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
121 122 123 |
# File 'lib/ecoportal/api/common/base_model.rb', line 121 def dirty? as_update != {} end |
#doc ⇒ Object
66 67 68 69 70 71 |
# File 'lib/ecoportal/api/common/base_model.rb', line 66 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
80 81 82 83 84 85 |
# File 'lib/ecoportal/api/common/base_model.rb', line 80 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
73 74 75 76 77 78 |
# File 'lib/ecoportal/api/common/base_model.rb', line 73 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
167 168 169 170 |
# File 'lib/ecoportal/api/common/base_model.rb', line 167 def print_pretty puts JSON.pretty_generate(as_json) self end |
#replace_doc!(new_doc) ⇒ Hash
It replaces doc
by new_doc
89 90 91 92 93 94 95 |
# File 'lib/ecoportal/api/common/base_model.rb', line 89 def replace_doc!(new_doc) raise UnlinkedModel.new(from: "#{self.class}#replace_doc", key: _key) unless linked? @doc.tap do @doc = new_doc end end |
#replace_original_doc!(new_doc) ⇒ Hash
It replaces original_doc
by new_doc
99 100 101 102 103 104 105 |
# File 'lib/ecoportal/api/common/base_model.rb', line 99 def replace_original_doc!(new_doc) raise UnlinkedModel.new(from: "#{self.class}#replace_original_doc", key: _key) unless linked? @original_doc.tap do @original_doc = new_doc end end |
#reset!(key = nil) ⇒ Object
- When there are nullable properties, it may be required to apply
reset!
from the parent i.e.parent.reset!("child")
# when parent.child isnil
- 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
.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ecoportal/api/common/base_model.rb', line 143 def reset!(key = nil) # rubocop:disable Metrics/AbcSize> msg = "'key' should be a String. Given #{key}" raise ArgumentError, msg unless !key || key.is_a?(String) raise UnlinkedModel.new(from: "#{self.class}#reset!", key: _key) unless linked? if key if respond_to?(key) && (child = 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)) # regenerate object if new_doc is null send(key) if !new_doc && 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
111 112 113 |
# File 'lib/ecoportal/api/common/base_model.rb', line 111 def to_json(*args) doc.to_json(*args) end |