Skip to content
This repository
Browse code

moved attribute translation to an object

  • Loading branch information...
commit 9c07e389be340e4d25298148cf39d10218ffb1a0 1 parent ee161d1
Aaron Patterson tenderlove authored
26 activerecord/lib/active_record/attribute_methods/read.rb
... ... @@ -1,3 +1,5 @@
  1 +require 'active_record/attributes/translator'
  2 +
1 3 module ActiveRecord
2 4 module AttributeMethods
3 5 module Read
@@ -126,28 +128,20 @@ def read_attribute(attr_name)
126 128 self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
127 129 end
128 130
129   -
130 131 private
131   - def cached_cast_attribute(attr_name, method)
132   - @attributes_cache[attr_name] ||= cast_attribute(attr_name, method)
133   - end
134   -
135   - def cast_attribute(attr_name, method)
136   - v = @attributes.fetch(attr_name) { missing_attribute(attr_name, caller) }
137   - v && send(method, attr_name, v)
138   - end
139 132
140   - def cast_serialized(attr_name, value)
141   - value.unserialized_value
  133 + def attribute_translator
  134 + Attributes::Translator.new(@attributes, @columns_hash)
142 135 end
143 136
144   - def cast_tz_conversion(attr_name, value)
145   - value = cast_column(attr_name, value)
146   - value.acts_like?(:time) ? value.in_time_zone : value
  137 + def cached_cast_attribute(attr_name, method)
  138 + @attributes_cache[attr_name] ||= cast_attribute(attr_name, method)
147 139 end
148 140
149   - def cast_column(attr_name, value)
150   - @columns_hash[attr_name].type_cast value
  141 + def cast_attribute(attr_name, method)
  142 + attribute_translator.cast_attribute(attr_name, method) do
  143 + missing_attribute(attr_name, caller)
  144 + end
151 145 end
152 146
153 147 def attribute(attribute_name)
28 activerecord/lib/active_record/attributes/translator.rb
... ... @@ -0,0 +1,28 @@
  1 +module ActiveRecord
  2 + module Attributes
  3 + class Translator # :nodoc:
  4 + def initialize(attributes, column_types)
  5 + @attributes = attributes
  6 + @column_types = column_types
  7 + end
  8 +
  9 + def cast_attribute(attr_name, method)
  10 + v = @attributes.fetch(attr_name) { yield }
  11 + v && send(method, attr_name, v)
  12 + end
  13 +
  14 + def cast_serialized(attr_name, value)
  15 + value.unserialized_value
  16 + end
  17 +
  18 + def cast_tz_conversion(attr_name, value)
  19 + value = cast_column(attr_name, value)
  20 + value.acts_like?(:time) ? value.in_time_zone : value
  21 + end
  22 +
  23 + def cast_column(attr_name, value)
  24 + @column_types[attr_name].type_cast value
  25 + end
  26 + end
  27 + end
  28 +end

0 comments on commit 9c07e38

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