Skip to content

Commit

Permalink
Resolved ticket #212.
Browse files Browse the repository at this point in the history
 * Made Property hashable by providing #hash and #eql? methods.
 * Made @dirty_attributes of Resource a Set to let users replace
   old dirty attributes with new ones.
  • Loading branch information
Martin Kihlgren committed Apr 17, 2008
1 parent bbb29f1 commit eeb1702
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/data_mapper/property.rb
Expand Up @@ -230,6 +230,18 @@ def field
@field ||= @options.fetch(:field, repository.adapter.field_naming_convention.call(name))
end

def hash
return @model.hash + @name.hash
end

def eql?(o)
if o.is_a?(Property)
return o.model == @model && o.name == @name
else
return false
end
end

def lazy?
@lazy
end
Expand Down
3 changes: 2 additions & 1 deletion lib/data_mapper/resource.rb
@@ -1,3 +1,4 @@
require 'set'
require __DIR__ + 'support/string'
require __DIR__ + 'property_set'
require __DIR__ + 'property'
Expand Down Expand Up @@ -103,7 +104,7 @@ def attribute_loaded?(name)
end

def dirty_attributes
@dirty_attributes ||= []
@dirty_attributes ||= Set.new
end

def dirty?
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/property_spec.rb
Expand Up @@ -18,6 +18,16 @@ class Tomato
end
end

it "should evaluate two similar properties as equal" do
p1 = DataMapper::Property.new(Zoo, :name, String, { :size => 30 })
p2 = DataMapper::Property.new(Zoo, :name, String, { :size => 30 })
p3 = DataMapper::Property.new(Zoo, :title, String, { :size => 30 })
p1.eql?(p2).should == true
p1.hash.should == p2.hash
p1.eql?(p3).should == false
p1.hash.should_not == p3.hash
end

it "should create a String property" do
property = DataMapper::Property.new(Zoo, :name, String, { :size => 30 })

Expand Down
11 changes: 11 additions & 0 deletions spec/unit/resource_spec.rb
Expand Up @@ -115,6 +115,17 @@ class Planet
pluto.attribute_dirty?(:age).should be_true
end

it 'should overwite old dirty attributes with new ones' do
pluto = Planet.new(:name => 'Pluto', :age => 500_000)
pluto.dirty_attributes.size.should == 2
pluto.attribute_dirty?(:name).should be_true
pluto.attribute_dirty?(:age).should be_true
pluto.name = "pluto"
pluto.dirty_attributes.size.should == 2
pluto.attribute_dirty?(:name).should be_true
pluto.attribute_dirty?(:age).should be_true
end

it 'should provide a key' do
Planet.new.should respond_to(:key)
end
Expand Down

0 comments on commit eeb1702

Please sign in to comment.