Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved OneToMany::Instance to ParentToChildAssociation

* Code was nearly identical in both modules.  Since OneToOne and
  OneToMany also need this code, I decided to use
  ParentToChildAssociation as the base
  • Loading branch information...
commit b9daabcaf74268db1e7b737bfec5b500441f5af5 1 parent a535182
Dan Kubb authored
View
65 lib/data_mapper/associations/one_to_many.rb
@@ -33,7 +33,7 @@ def #{name}_association
@#{name}_association ||= begin
relationship = self.class.relationships[:#{name}]
- association = relationship.with_parent(self, Instance) do |repository, child_key, parent_key, child_model, parent_resource|
+ association = relationship.with_parent(self, Associations::ParentToChildAssociation) do |repository, child_key, parent_key, child_model, parent_resource|
repository.all(child_model, child_key.to_query(parent_key.get(parent_resource)))
end
@@ -46,69 +46,6 @@ def #{name}_association
relationships[name]
end
-
- class Instance
- extend Forwardable
-
- def_delegators :children, :[], :size, :length, :first, :last
-
- include Enumerable
-
- def each
- children.each { |child| yield(child) }
- end
-
- def children
- @children_resources ||= @children_loader.call
- end
-
- def save
- @dirty_children.each do |child_resource|
- @relationship.attach_parent(child_resource, @parent_resource)
- repository(@relationship.repository_name).save(child_resource)
- end
- end
-
- def push(*child_resources)
- child_resources.each do |child_resource|
- children << child_resource
-
- if @parent_resource.new_record?
- @dirty_children << child_resource
- else
- @relationship.attach_parent(child_resource, @parent_resource)
- repository(@relationship.repository_name).save(child_resource)
- end
- end
-
- self
- end
-
- alias << push
-
- def delete(child_resource)
- deleted_resource = children.delete(child_resource)
- begin
- @relationship.attach_parent(deleted_resource, nil)
- repository(@relationship.repository_name).save(deleted_resource)
- rescue
- children << child_resource
- raise
- end
- end
-
- private
-
- def initialize(relationship, parent_resource, &children_loader)
-# raise ArgumentError, "+relationship+ should be a DataMapper::Association::Relationship, but was #{relationship.class}", caller unless Relationship === relationship
-# raise ArgumentError, "+parent_resource+ should be a DataMapper::Resource, but was #{parent_resource.class}", caller unless Resource === parent_resource
-
- @relationship = relationship
- @parent_resource = parent_resource
- @children_loader = children_loader
- @dirty_children = []
- end
- end # class Instance
end # module OneToMany
end # module Associations
end # module DataMapper
View
2  lib/data_mapper/associations/one_to_one.rb
@@ -37,7 +37,7 @@ def #{name}_association
@#{name}_association ||= begin
relationship = self.class.relationships[:#{name}]
- association = relationship.with_parent(self, Associations::OneToMany::Instance) do |repository, child_key, parent_key, child_model, parent_resource|
+ association = relationship.with_parent(self, Associations::ParentToChildAssociation) do |repository, child_key, parent_key, child_model, parent_resource|
repository.all(child_model, child_key.to_query(parent_key.get(parent_resource)))
end
View
9 lib/data_mapper/associations/parent_to_child_association.rb
@@ -4,9 +4,14 @@ module DataMapper
module Associations
class ParentToChildAssociation
extend Forwardable
+ include Enumerable
def_delegators :children, :[], :size, :length, :first, :last
+ def each
+ children.each { |child| yield(child) }
+ end
+
def children
@children_resources ||= @children_loader.call
end
@@ -49,8 +54,8 @@ def delete(child_resource)
private
def initialize(relationship, parent_resource, &children_loader)
- raise ArgumentError, "+name+ should be a Symbol, but was #{name.class}", caller unless Relationsip === relationship
- raise ArgumentError, "+parent_resource+ should be a Resource, but was #{parent_resource.class}", caller unless Resource === parent_resource
+# raise ArgumentError, "+relationship+ should be a DataMapper::Association::Relationship, but was #{relationship.class}", caller unless Relationship === relationship
+# raise ArgumentError, "+parent_resource+ should be a DataMapper::Resource, but was #{parent_resource.class}", caller unless Resource === parent_resource
@relationship = relationship
@parent_resource = parent_resource
Please sign in to comment.
Something went wrong with that request. Please try again.