Sha256: 2b5b16144432c1d407e6b124a78fc98e1a3e654b17a159642e684df1785187e8

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'active_data/model/associations/association'
require 'active_data/model/associations/embeds_many'
require 'active_data/model/associations/embeds_one'

module ActiveData
  module Model
    module Associations
      extend ActiveSupport::Concern

      included do
        class_attribute :_associations, instance_reader: false, instance_writer: false
        self._associations = {}

        { embeds_many: EmbedsMany, embeds_one: EmbedsOne }.each do |(name, association_class)|
          define_singleton_method name do |*args|
            association = association_class.new *args
            association.define_accessor self
            self._associations = _associations.merge(association.name => association)
          end
        end
      end

      module ClassMethods

        def reflect_on_association name
          _associations[name.to_s]
        end

        def associations
          _associations
        end

        def association_names
          _associations.keys
        end
      end

      def == other
        super(other) && self.class.association_names.all? do |association|
          send(association) == other.send(association)
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_data-0.3.0 lib/active_data/model/associations.rb
active_data-0.2.0 lib/active_data/model/associations.rb