Skip to content

Commit

Permalink
minor refactor to casting code to better match what the MysqlAdapter …
Browse files Browse the repository at this point in the history
…does, removing unneeded checks
  • Loading branch information
brianmario committed May 13, 2010
1 parent 1f7ff62 commit 6b285dc
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/active_record/connection_adapters/mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,32 @@ def klass
def type_cast(value)
return nil if value.nil?
case type
when :string then value
when :text then value
when :integer then value.is_a?(Fixnum) ? value : (value.to_i rescue value ? 1 : 0)
when :float then value.class == Float ? value : value.to_f
when :decimal then value.class == BigDecimal ? value : self.class.value_to_decimal(value)
when :datetime then value.class == Time ? value : self.class.string_to_time(value)
when :timestamp then value.class == Time ? value : self.class.string_to_time(value)
when :time then value.class == Time ? value : self.class.string_to_dummy_time(value)
when :date then value.class == Date ? value : self.class.string_to_date(value)
when :binary then value
when :boolean then self.class.value_to_boolean(value)
when :string then value
when :text then value
when :integer then value.to_i rescue value ? 1 : 0
when :float then value.to_f # returns self if it's already a Float
when :decimal then self.class.value_to_decimal(value)
when :datetime, :timestamp then value.class == Time ? value : self.class.string_to_time(value)
when :time then value.class == Time ? value : self.class.string_to_dummy_time(value)
when :date then value.class == Date ? value : self.class.string_to_date(value)
when :binary then value
when :boolean then self.class.value_to_boolean(value)
else value
end
end

def type_cast_code(var_name)
case type
when :string then nil
when :text then nil
when :integer then "(#{var_name}.is_a?(Fixnum) ? #{var_name} : (#{var_name}.to_i rescue value ? 1 : 0))"
when :float then "#{var_name}.class == Float ? #{var_name} : #{var_name}.to_f"
when :decimal then "#{var_name}.class == BigDecimal ? #{var_name} : #{self.class.name}.value_to_decimal(#{var_name})"
when :datetime then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})"
when :timestamp then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})"
when :time then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_dummy_time(#{var_name})"
when :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})"
when :binary then nil
when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
when :string then nil
when :text then nil
when :integer then "#{var_name}.to_i rescue value ? 1 : 0)"
when :float then "#{var_name}.to_f"
when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
when :datetime, :timestamp then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})"
when :time then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_dummy_time(#{var_name})"
when :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})"
when :binary then nil
when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
else nil
end
end
Expand Down

0 comments on commit 6b285dc

Please sign in to comment.