Class: Ecoportal::API::Common::BaseModel

Inherits:
Object
  • Object
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
53
# 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)
    @initial_doc  = JSON.parse(@doc.to_json)
  end
end

Instance Attribute Details

#_keyObject (readonly)

Returns the value of attribute _key.



43
44
45
# File 'lib/ecoportal/api/common/base_model.rb', line 43

def _key
  @_key
end

#_parentObject (readonly)

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_jsonObject



73
74
75
# File 'lib/ecoportal/api/common/base_model.rb', line 73

def as_json
  doc
end

#as_update(ref = :last) ⇒ Object



81
82
83
84
85
# File 'lib/ecoportal/api/common/base_model.rb', line 81

def as_update(ref = :last)
  new_doc = as_json
  ref_doc = ref == :total ? initial_doc : original_doc
  Common::HashDiff.diff(new_doc, ref_doc)
end

#consolidate!Object

Raises:



91
92
93
94
95
96
97
98
99
# File 'lib/ecoportal/api/common/base_model.rb', line 91

def consolidate!
  raise UnlinkedModel.new  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

Returns:

  • (Boolean)


87
88
89
# File 'lib/ecoportal/api/common/base_model.rb', line 87

def dirty?
  as_update != {}
end

#docObject

Raises:



55
56
57
58
59
# File 'lib/ecoportal/api/common/base_model.rb', line 55

def doc
  raise UnlinkedModel.new  unless linked?
  return @doc if is_root?
  _parent.doc.dig(*[_key].flatten)
end

#initial_docObject

Raises:



67
68
69
70
71
# File 'lib/ecoportal/api/common/base_model.rb', line 67

def initial_doc
  raise UnlinkedModel.new  unless linked?
  return @initial_doc if is_root?
  _parent.initial_doc.dig(*[_key].flatten)
end

#original_docObject

Raises:



61
62
63
64
65
# File 'lib/ecoportal/api/common/base_model.rb', line 61

def original_doc
  raise UnlinkedModel.new  unless linked?
  return @original_doc if is_root?
  _parent.original_doc.dig(*[_key].flatten)
end


111
112
113
114
# File 'lib/ecoportal/api/common/base_model.rb', line 111

def print_pretty
  puts JSON.pretty_generate(as_json)
  self
end

#reset!Object

Raises:



101
102
103
104
105
106
107
108
109
# File 'lib/ecoportal/api/common/base_model.rb', line 101

def reset!
  raise UnlinkedModel.new  unless linked?
  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

#to_json(*args) ⇒ Object



77
78
79
# File 'lib/ecoportal/api/common/base_model.rb', line 77

def to_json(*args)
  doc.to_json(*args)
end