Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/krobertson/dm-core
Browse files Browse the repository at this point in the history
Conflicts:

	lib/data_mapper/property.rb
	lib/data_mapper/resource.rb
  • Loading branch information
sam committed Apr 17, 2008
2 parents e6e61fc + adecabc commit d954e50
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/data_mapper/resource.rb
Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions lib/data_mapper/type.rb
Expand Up @@ -27,7 +27,7 @@ module DataMapper
# primitive String
# size 10
#
# def self.dump(value)
# def self.dump(value, property)
# <work some magic>
# end
#
Expand Down Expand Up @@ -123,28 +123,32 @@ 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
#
# ==== 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

Expand Down
4 changes: 2 additions & 2 deletions lib/data_mapper/types/csv.rb
Expand Up @@ -5,15 +5,15 @@ 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
else nil
end
end

def self.dump(value)
def self.dump(value, property)
case value
when Array then
FasterCSV.generate do |csv|
Expand Down
4 changes: 2 additions & 2 deletions lib/data_mapper/types/yaml.rb
Expand Up @@ -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)
Expand All @@ -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 =~ /^---/
Expand Down
56 changes: 50 additions & 6 deletions spec/unit/type_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d954e50

Please sign in to comment.