Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improve property-setting

  • Loading branch information...
commit 57ef16586df73ab5c7fc153e83295f0cd3907646 1 parent b5656ea
Yehuda Katz authored

Showing 2 changed files with 22 additions and 3 deletions. Show diff stats Hide diff stats

  1. +18 0 lib/data_mapper/property.rb
  2. +4 3 lib/data_mapper/resource.rb
18 lib/data_mapper/property.rb
@@ -261,6 +261,24 @@ def set(value, resource)
261 261 def inspect
262 262 "#<Property:#{@model}:#{@name}>"
263 263 end
  264 +
  265 + def typecast(value)
  266 + if type == TrueClass
  267 + value == true || value == "true"
  268 + elsif type == String
  269 + value.to_s
  270 + elsif [Float, Fixnum, BigDecimal].include?(type)
  271 + value.to_f
  272 + elsif type == DateTime
  273 + Time.parse(value)
  274 + elsif type == Date
  275 + Date.parse(value)
  276 + elsif type == Class
  277 + Object.recursive_const_get(value)
  278 + else
  279 + value
  280 + end
  281 + end
264 282
265 283 private
266 284
7 lib/data_mapper/resource.rb
@@ -57,7 +57,7 @@ def []=(name, value)
57 57 end
58 58
59 59 dirty_attributes << property
60   - instance_variable_set(ivar_name, property.custom? ? property.type.dump(value) : value)
  60 + instance_variable_set(ivar_name, property.custom? ? property.type.dump(value) : property.typecast(value))
61 61 end
62 62
63 63 def repository
@@ -144,11 +144,12 @@ def attributes
144 144 # Mass-assign mapped fields.
145 145 def attributes=(values_hash)
146 146 values_hash.each_pair do |k,v|
147   - setter = "#{k.to_s.sub(/\?\z/, '')}="
  147 + setter = k.to_s.sub(/\?\z/, '')
148 148 # We check #public_methods and not Class#public_method_defined? to
149 149 # account for singleton methods.
150 150 if public_methods.include?(setter)
151   - send(setter, v)
  151 + self[setter] = v
  152 + # send(setter, v)
152 153 end
153 154 end
154 155 end

0 comments on commit 57ef165

Please sign in to comment.
Something went wrong with that request. Please try again.