Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of git://github.com/krobertson/dm-core

Conflicts:

	lib/data_mapper/property.rb
	lib/data_mapper/resource.rb
  • Loading branch information...
commit d954e5021198c48157ff68b872013d42c3feb939 2 parents e6e61fc + adecabc
@sam sam authored
View
5 lib/data_mapper/resource.rb
@@ -46,7 +46,7 @@ def [](name)
end
value = instance_variable_get(ivar_name)
- property.custom? ? property.type.load(value) : value
+ property.custom? ? property.type.load(value, property) : value
end
def []=(name, value)
@@ -58,7 +58,8 @@ def []=(name, value)
end
dirty_attributes << property
- instance_variable_set(ivar_name, property.custom? ? property.type.dump(value) : property.typecast(value))
+
+ instance_variable_set(ivar_name, property.custom? ? property.type.dump(value, property) : property.typecast(value))
end
def repository
View
12 lib/data_mapper/type.rb
@@ -27,7 +27,7 @@ module DataMapper
# primitive String
# size 10
#
- # def self.dump(value)
+ # def self.dump(value, property)
# <work some magic>
# end
#
@@ -123,14 +123,16 @@ def options
# ==== Parameters
# value<Object, nil>::
# The value to dump
+ # property<Property, nil>::
+ # The property the type is being used by
#
# ==== Returns
# Object:: Dumped object
#
#
# @public
- def self.dump(value)
- value
+ def self.dump(value, property)
+ value
end
# Stub instance method for loading
@@ -138,13 +140,15 @@ def self.dump(value)
# ==== Parameters
# value<Object, nil>::
# The value to serialize
+ # property<Property, nil>::
+ # The property the type is being used by
#
# ==== Returns
# Object:: Serialized object. Must be the same type as the ruby primitive
#
#
# @public
- def self.load(value)
+ def self.load(value, property)
value
end
View
4 lib/data_mapper/types/csv.rb
@@ -5,7 +5,7 @@ class Csv < DataMapper::Type
size 65535
lazy true
- def self.load(value)
+ def self.load(value, property)
case value
when String then FasterCSV.parse(value)
when Array then value
@@ -13,7 +13,7 @@ def self.load(value)
end
end
- def self.dump(value)
+ def self.dump(value, property)
case value
when Array then
FasterCSV.generate do |csv|
View
4 lib/data_mapper/types/yaml.rb
@@ -7,7 +7,7 @@ class Yaml < DataMapper::Type
size 65535
lazy true
- def self.load(value)
+ def self.load(value, property)
if value.nil?
nil
elsif value.is_a?(String)
@@ -17,7 +17,7 @@ def self.load(value)
end
end
- def self.dump(value)
+ def self.dump(value, property)
if value.nil?
nil
elsif value.is_a?(String) && value =~ /^---/
View
56 spec/unit/type_spec.rb
@@ -15,14 +15,35 @@ class TestType2 < DataMapper::Type
primitive String
size 10
- def self.load(value)
+ def self.load(value, property)
value.reverse
end
- def self.dump(value)
+ def self.dump(value, property)
value.reverse
end
end
+
+ class TestResource
+ include DataMapper::Resource
+ end
+
+ class TestType3 < DataMapper::Type
+ primitive String
+ size 10
+ attr_accessor :property, :value
+
+ def self.load(value, property)
+ type = self.new
+ type.property = property
+ type.value = value
+ type
+ end
+
+ def self.dump(value, property)
+ value.value
+ end
+ end
end
it "should have the same PROPERTY_OPTIONS aray as DataMapper::Property" do
@@ -48,19 +69,42 @@ def self.dump(value)
end
it "should pass through the value if load wasn't overriden" do
- TestType.load("test").should == "test"
+ TestType.load("test", nil).should == "test"
end
it "should pass through the value if dump wasn't overriden" do
- TestType.dump("test").should == "test"
+ TestType.dump("test", nil).should == "test"
end
it "should not raise NotImplmenetedException if load was overriden" do
- TestType2.dump("helo").should == "oleh"
+ TestType2.dump("helo", nil).should == "oleh"
end
it "should not raise NotImplmenetedException if dump was overriden" do
- TestType2.load("oleh").should == "helo"
+ TestType2.load("oleh", nil).should == "helo"
+ end
+
+ describe "using a custom type" do
+ before do
+ @property = DataMapper::Property.new TestResource, :name, TestType3, {}
+ end
+
+ it "should return a object of the same type" do
+ TestType3.load("helo", @property).class.should == TestType3
+ end
+
+ it "should contain the property" do
+ TestType3.load("helo", @property).property.should == @property
+ end
+
+ it "should contain the value" do
+ TestType3.load("helo", @property).value.should == "helo"
+ end
+
+ it "should return the value" do
+ obj = TestType3.load("helo", @property)
+ TestType3.dump(obj, @property).should == "helo"
+ end
end
describe "using def Type" do
Please sign in to comment.
Something went wrong with that request. Please try again.