Sha256: 03239e68f6d8c508a50543b62acc9a5d32c4d09bc3c013e45dffb74c9e0ff82c

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ooze-parser-0.1.11 lib/ooz/common/base_model.rb
ooze-parser-0.1.10 lib/ooz/common/base_model.rb
ooze-parser-0.1.9 lib/ooz/common/base_model.rb
ooze-parser-0.1.8 lib/ooz/common/base_model.rb
ooze-parser-0.1.7 lib/ooz/common/base_model.rb