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

.passthrough(*methods, to:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ecoportal/api/common/base_model.rb', line 29

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



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

def as_json
  doc
end

#as_updateObject



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

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


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

def print
  puts JSON.pretty_generate(as_json)
  self
end

#to_json(*args) ⇒ Object



20
21
22
# File 'lib/ecoportal/api/common/base_model.rb', line 20

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