Sha256: 43be5dc0df58c18b00daf64d0e80263b2674c3af16a4f73b070e3f5bf4cf94d5

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 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 identifier_hash
      {
        type: self.class::TYPE,
        id: @id
      }.compact
    end

    def to_h_jsonapi
      identifier_hash.merge({
        attributes: attributes,
        relationships: associations.map do |k, v|
          if v.is_a?(Array)
            [k, { data: v.map(&:to_h_jsonapi) }]
          else
            [k, { data: v.to_h_jsonapi }]
          end
        end.to_h,
      }).merge(meta_hash)
      .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 associations_hash
      associations.map do |k, v|
        if v.is_a?(Array)
          [k, v.map(&:to_h)]
        else
          [k, v.to_h]
        end
      end.to_h
    end

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

    def meta_hash
      {
        meta: meta
      }
    end

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

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

    def to_h
      identifier_hash
        .merge(@properties)
        .merge(meta_hash)
    end

    def assign_attributes(attributes)
      @properties.merge!(attributes)
    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.1.0 lib/chronicle/schema/base.rb