module Ecoportal module API module Common class BaseModel attr_reader :doc def initialize(doc = {}) @doc = doc @original_doc = JSON.parse(doc.to_json) end def self.build(doc = {}) new(doc).tap do |instance| instance.instance_variable_set("@original_doc", {}) end end def print puts JSON.pretty_generate(as_json) self end def as_json doc end def to_json(*args) doc.to_json(*args) end def as_update new_doc = as_json Common::HashDiff.diff(new_doc, @original_doc) end 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 end end end end