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

Inherits:
Object
  • Object
show all
Defined in:
lib/ecoportal/api/common/base_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



6
7
8
9
# File 'lib/ecoportal/api/common/base_model.rb', line 6

def initialize(doc = {})
  @doc          = doc
  @original_doc = JSON.parse(doc.to_json)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



5
6
7
# File 'lib/ecoportal/api/common/base_model.rb', line 5

def doc
  @doc
end

Class Method Details

.build(doc = {}) ⇒ Object



11
12
13
14
15
# File 'lib/ecoportal/api/common/base_model.rb', line 11

def self.build(doc = {})
  new(doc).tap do |instance|
    instance.instance_variable_set("@original_doc", {})
  end
end

.passthrough(*methods, to:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ecoportal/api/common/base_model.rb', line 35

def self.passthrough(*methods, to:)
  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



22
23
24
# File 'lib/ecoportal/api/common/base_model.rb', line 22

def as_json
  doc
end

#as_updateObject



30
31
32
33
# File 'lib/ecoportal/api/common/base_model.rb', line 30

def as_update
  new_doc = as_json
  Common::HashDiff.diff(new_doc, @original_doc)
end


17
18
19
20
# File 'lib/ecoportal/api/common/base_model.rb', line 17

def print
  puts JSON.pretty_generate(as_json)
  self
end

#to_json(*args) ⇒ Object



26
27
28
# File 'lib/ecoportal/api/common/base_model.rb', line 26

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