Permalink
Browse files

Updated LoadedSet API to match "Set" API for associations

* Refactored LoadedSet#add to use LoadedSet#push to add new elements
  to the Set
  • Loading branch information...
Dan Kubb
Dan Kubb committed Apr 19, 2008
1 parent c528f80 commit a51412a4fd7edd5b58fc45ffd2ea9d0e4bf7fe1c
@@ -6,10 +6,10 @@ class ParentToChildAssociation
extend Forwardable
include Enumerable
- def_delegators :children, :[], :size, :length, :first, :last
+ def_instance_delegators :entries, :[], :size, :length, :first, :last
- def each
- children.each { |child| yield(child) }
+ def loaded?
+ !defined?(@children_resources)
end
def children
@@ -50,6 +50,15 @@ def delete(child_resource)
end
end
+ def clear
+ each { |child_resource| delete(child_resource) }
+ end
+
+ def each(&block)
+ children.each { |child_resource| yield child_resource }
+ self
+ end
+
private
def initialize(relationship, parent_resource, &children_loader)
@@ -1,16 +1,11 @@
+require 'forwardable'
+
module DataMapper
class LoadedSet
- attr_reader :repository
-
- def keys
- entry_keys = @entries.map { |resource| resource.key }
+ extend Forwardable
+ include Enumerable
- keys = {}
- @key_properties.zip(entry_keys.transpose).each do |property,values|
- keys[property] = values
- end
- keys
- end
+ def_instance_delegators :entries, :[], :size, :length, :first, :last
def reload!(options = {})
query = Query.new(@model, keys.merge(:fields => @key_properties))
@@ -31,13 +26,11 @@ def add(values, reload = false)
key_values = values.values_at(*@key_property_indexes)
if resource = @repository.identity_map_get(model, key_values)
- @entries << resource
- resource.loaded_set = self
+ self << resource
return resource unless reload
else
resource = model.allocate
- @entries << resource
- resource.loaded_set = self
+ self << resource
@key_properties.zip(key_values).each do |property,key_value|
resource.instance_variable_set(property.instance_variable_name, key_value)
end
@@ -46,8 +39,7 @@ def add(values, reload = false)
end
else
resource = model.allocate
- @entries << resource
- resource.loaded_set = self
+ self << resource
resource.instance_variable_set(:@new_record, false)
resource.readonly!
end
@@ -59,15 +51,27 @@ def add(values, reload = false)
self
end
- alias << add
+ def push(*resources)
+ resources.each do |resource|
+ @resources << resource
+ resource.loaded_set = self
+ end
+ self
+ end
+
+ alias << push
- def first
- @entries.first
+ def delete(resource)
+ @resources.delete(resource)
end
def entries
- @entries.uniq!
- @entries.dup
+ @resources.dup
+ end
+
+ def each(&block)
+ entries.each { |entry| yield entry }
+ self
end
private
@@ -81,7 +85,7 @@ def initialize(repository, model, properties_with_indexes)
@repository = repository
@model = model
@properties_with_indexes = properties_with_indexes
- @entries = []
+ @resources = []
if inheritance_property = @model.inheritance_property(@repository.name)
@inheritance_property_index = @properties_with_indexes[inheritance_property]
@@ -91,13 +95,19 @@ def initialize(repository, model, properties_with_indexes)
@key_property_indexes = @properties_with_indexes.values_at(*@key_properties)
end
end
- end # class LoadedSet
- class LazyLoadedSet < LoadedSet
- def each(&block)
- entries.each { |entry| yield entry }
+ def keys
+ entry_keys = @resources.map { |resource| resource.key }
+
+ keys = {}
+ @key_properties.zip(entry_keys.transpose).each do |property,values|
+ keys[property] = values
+ end
+ keys
end
+ end # class LoadedSet
+ class LazyLoadedSet < LoadedSet
def entries
@loader[self]
@@ -28,6 +28,7 @@ def identity_map_set(resource)
@identity_map.set(resource)
end
+ # TODO: this should use current_scope too
def get(model, key)
@identity_map.get(model, key) || @adapter.read(self, model, key)
end
@@ -142,7 +142,7 @@ def reload!
# Returns <tt>true</tt> if this model hasn't been saved to the
# database, <tt>false</tt> otherwise.
def new_record?
- @new_record.nil? || @new_record
+ !defined?(@new_record) || @new_record
end
def attributes
@@ -77,7 +77,6 @@
lls.add(['Nancy', 11])
end
- set.instance_variable_get("@entries").should be_empty
results = set.entries
results.size.should == 2
end
View
@@ -170,6 +170,12 @@ class Tomahto
property.typecast(nil).should == nil
end
+ it 'should pass through the value for an Object property' do
+ value = 'a ruby object'
+ property = DataMapper::Property.new(Zoo, :object, Object)
+ property.typecast(value).object_id.should == value.object_id
+ end
+
it 'should typecast value (true) for a TrueClass property' do
property = DataMapper::Property.new(Zoo, :true_class, TrueClass)
property.typecast(true).should == true
@@ -215,12 +221,6 @@ class Tomahto
property.typecast('Zoo').should == Zoo
end
- it 'should pass through the value for an Object property' do
- value = 'a ruby object'
- property = DataMapper::Property.new(Zoo, :object, Object)
- property.typecast(value).object_id.should == value.object_id
- end
-
it 'should provide inspect' do
DataMapper::Property.new(Zoo, :name, String).should respond_to(:inspect)
end
View
@@ -191,7 +191,7 @@ class Moon
end
it '.storage_name should use default repository when not passed any arguments' do
- Planet.storage_name.should == Planet.storage_name(:default)
+ Planet.storage_name.object_id.should == Planet.storage_name(:default).object_id
end
it 'should provide storage_names' do
@@ -218,6 +218,10 @@ class Moon
Planet.properties(:legacy).should have(6).entries
end
+ it '.properties should use default repository when not passed any arguments' do
+ Planet.properties.object_id.should == Planet.properties(:default).object_id
+ end
+
it 'should provide key' do
Planet.should respond_to(:key)
end
@@ -229,7 +233,7 @@ class Moon
end
it '.key should use default repository when not passed any arguments' do
- Planet.key.should == Planet.key(:default)
+ Planet.key.object_id.should == Planet.key(:default).object_id
end
it 'should provide inheritance_property' do
@@ -243,7 +247,7 @@ class Moon
end
it '.inheritance_property should use default repository when not passed any arguments' do
- Planet.inheritance_property.should == Planet.inheritance_property(:default)
+ Planet.inheritance_property.object_id.should == Planet.inheritance_property(:default).object_id
end
it 'should provide finder methods' do

0 comments on commit a51412a

Please sign in to comment.