Sha256: 3ffdea7c7594bbb4bb970f3718755146c74ea9572f703d30c33834752223104b

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module ActiverecordToPoro
  SourceObjectInfo = Yaoc::Helper::StructH(:class_name, :column, :value, :lock_version, :object_id) do

    include Equalizer.new(:class_name, :column, :value)

    def to_hash
      {
       class_name: class_name,
       primary_key: {column: column, value: value},
       object_id: object_id,
       lock_version: lock_version
      }
    end

    def as_scope_hash
      result = column.nil? ? {} : {column => value}
      result[:lock_version] = lock_version unless lock_version.nil?

      result
    end
  end

  class Metadata
    attr_accessor :source_object_info

    def initialize()
      self.source_object_info = Set.new()
    end

    def initialize_from_ar(ar_object=nil)
      unless ar_object.nil?
        set_source_info(ar_object)
      end
    end

    def for_ar_class(ar_class_name)
      source_object_info.find(->{SourceObjectInfo.new}){|data|
        data.class_name == ar_class_name
      }
    end

    def set_source_info(ar_object)
      new_info = create_source_info(ar_object)
      self.source_object_info.delete_if { |info| info == new_info }
      self.source_object_info << new_info
    end

    def create_source_info(ar_object)
      SourceObjectInfo.new(class_name: ar_object.class.name,
                           column: ar_object.class.primary_key,
                           value: ar_object.send(ar_object.class.primary_key),
                           object_id: ar_object.object_id,
                           lock_version: ar_object.respond_to?(:lock_version) ? ar_object.lock_version : nil
      )
    end

    def to_hash
      { source_objects_info: source_object_info.map(&:to_hash)  }
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activerecord_to_poro-0.0.9 lib/activerecord_to_poro/metadata.rb
activerecord_to_poro-0.0.8 lib/activerecord_to_poro/metadata.rb
activerecord_to_poro-0.0.7 lib/activerecord_to_poro/metadata.rb
activerecord_to_poro-0.0.6 lib/activerecord_to_poro/metadata.rb