module Ooz module Common class BaseModel < BaseClass attr_reader :doc def initialize(doc = {}) @doc = doc @original_doc = JSON.parse(doc.to_json) @last_change = {} 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 consolidate! @original_doc = JSON.parse(@doc.to_json) end def undo! @doc = JSON.parse(@original_doc.to_json) end def as_update new_doc = as_json Common::HashDiff.diff(new_doc, @original_doc) end def changed? !as_update.empty? end def new_change? !Common::HashDiff.diff(as_update, @last_change).empty? end def update_last_change! last_chg = @last_change @last_change = as_update last_chg end end end end