Sha256: 12e296c8674495c17e812441be6949e254aa5d7b2d8309dea44ade7de05673b7

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'chronicle/utils/hash'

module Chronicle::Schema
  class Base
    attr_accessor(:id, :dedupe_on, :properties)

    def initialize(attributes = {})
      @properties = {}
      @dedupe_on = []
      assign_attributes(attributes) if attributes
    end

    def method_missing(method, *args)
      if method.to_s.end_with?("=")
        @properties[method.to_s.gsub("=", "").to_sym] = args.first
      else
        @properties[method]
      end
    end

    def assign_attributes(attributes)
      @properties.merge!(attributes)
    end

    def identifier_hash
      {
        id: @id,
        type: self.class::TYPE
      }.compact
    end

    def attributes
      @properties.reject { |k, v| associations.keys.include?(k) }.compact
    end

    def associations
      @properties.select do |k, v|
        if v.is_a?(Array)
          v.any? { |e| e.is_a?(Chronicle::Schema::Base) }
        else
          v.is_a?(Chronicle::Schema::Base)
        end
      end
    end

    def meta
      {
        dedupe_on: @dedupe_on.map{|d| d.map(&:to_s).join(",")}
      }
    end

    def to_h
      @properties
    end

    def to_h_flattened
      Chronicle::Utils::Hash.flatten_keys(to_h)
    end
  end
end

require_relative "activity"
require_relative "entity"
require_relative "raw"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-core-0.2.1 lib/chronicle/schema/base.rb