Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve property-setting

  • Loading branch information...
commit 57ef16586df73ab5c7fc153e83295f0cd3907646 1 parent b5656ea
@wycats wycats authored
Showing with 22 additions and 3 deletions.
  1. +18 −0 lib/data_mapper/property.rb
  2. +4 −3 lib/data_mapper/resource.rb
View
18 lib/data_mapper/property.rb
@@ -261,6 +261,24 @@ def set(value, resource)
def inspect
"#<Property:#{@model}:#{@name}>"
end
+
+ def typecast(value)
+ if type == TrueClass
+ value == true || value == "true"
+ elsif type == String
+ value.to_s
+ elsif [Float, Fixnum, BigDecimal].include?(type)
+ value.to_f
+ elsif type == DateTime
+ Time.parse(value)
+ elsif type == Date
+ Date.parse(value)
+ elsif type == Class
+ Object.recursive_const_get(value)
+ else
+ value
+ end
+ end
private
View
7 lib/data_mapper/resource.rb
@@ -57,7 +57,7 @@ def []=(name, value)
end
dirty_attributes << property
- instance_variable_set(ivar_name, property.custom? ? property.type.dump(value) : value)
+ instance_variable_set(ivar_name, property.custom? ? property.type.dump(value) : property.typecast(value))
end
def repository
@@ -144,11 +144,12 @@ def attributes
# Mass-assign mapped fields.
def attributes=(values_hash)
values_hash.each_pair do |k,v|
- setter = "#{k.to_s.sub(/\?\z/, '')}="
+ setter = k.to_s.sub(/\?\z/, '')
# We check #public_methods and not Class#public_method_defined? to
# account for singleton methods.
if public_methods.include?(setter)
- send(setter, v)
+ self[setter] = v
+ # send(setter, v)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.