Sha256: 90d93f479adc67711f4b741fab38b5ed18d6f17194bdba5ab0a9acda7da66857

Contents?: true

Size: 1.04 KB

Versions: 11

Compression:

Stored size: 1.04 KB

Contents

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ecoportal-api-0.3.8 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.6 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.5 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.4 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.3 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.1 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.0 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.3.0.pre1 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.2.2 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.2.1 lib/ecoportal/api/common/base_model.rb
ecoportal-api-0.2.0 lib/ecoportal/api/common/base_model.rb