aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attributes
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-01-31 15:17:31 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-02-07 13:51:52 -0800
commit9c07e389be340e4d25298148cf39d10218ffb1a0 (patch)
treec2fcf4bbab2c702c065cf515c5f894bcf1e27f1d /activerecord/lib/active_record/attributes
parentee161d1bc01a761abf5473d5a8d53f8684be93e9 (diff)
downloadrails-9c07e389be340e4d25298148cf39d10218ffb1a0.tar.gz
rails-9c07e389be340e4d25298148cf39d10218ffb1a0.tar.bz2
rails-9c07e389be340e4d25298148cf39d10218ffb1a0.zip
moved attribute translation to an object
Diffstat (limited to 'activerecord/lib/active_record/attributes')
-rw-r--r--activerecord/lib/active_record/attributes/translator.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/attributes/translator.rb b/activerecord/lib/active_record/attributes/translator.rb
new file mode 100644
index 0000000000..62fb874215
--- /dev/null
+++ b/activerecord/lib/active_record/attributes/translator.rb
@@ -0,0 +1,28 @@
+module ActiveRecord
+ module Attributes
+ class Translator # :nodoc:
+ def initialize(attributes, column_types)
+ @attributes = attributes
+ @column_types = column_types
+ end
+
+ def cast_attribute(attr_name, method)
+ v = @attributes.fetch(attr_name) { yield }
+ v && send(method, attr_name, v)
+ end
+
+ def cast_serialized(attr_name, value)
+ value.unserialized_value
+ end
+
+ def cast_tz_conversion(attr_name, value)
+ value = cast_column(attr_name, value)
+ value.acts_like?(:time) ? value.in_time_zone : value
+ end
+
+ def cast_column(attr_name, value)
+ @column_types[attr_name].type_cast value
+ end
+ end
+ end
+end