lib/active_data/model/associations.rb in active_data-0.1.0 vs lib/active_data/model/associations.rb in active_data-0.2.0
- old
+ new
@@ -1,49 +1,46 @@
-require 'active_data/model/associations/many'
+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 = ActiveSupport::HashWithIndifferentAccess.new
+ 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]
+ _associations[name.to_s]
end
def associations
_associations
end
def association_names
_associations.keys
end
+ end
- def embeds_many name, options = {}
- association = Many.new name, options
- define_collection_reader association
- define_collection_writer association
- self._associations = _associations.merge!(association.name => association)
+ def == other
+ super(other) && self.class.association_names.all? do |association|
+ send(association) == other.send(association)
end
-
- def define_collection_reader association
- define_method association.name do
- instance_variable_get("@#{association.name}") || association.klass.collection([])
- end
- end
-
- def define_collection_writer association
- define_method "#{association.name}=" do |value|
- instance_variable_set "@#{association.name}", association.klass.collection(value)
- end
- end
-
end
+
end
end
end
\ No newline at end of file