Using Classes instead of strings for typecasting and removing redundant comparisons

This commit is contained in:
Sam Lown 2010-03-30 19:06:24 +00:00
parent e922b81ccc
commit 64d68ecc1a
2 changed files with 12 additions and 17 deletions

View file

@ -57,6 +57,7 @@ module CouchRest
associate_casted_to_parent(value, assigned) associate_casted_to_parent(value, assigned)
value value
end end
# only cast arrays of more complex objects (i.e. not strings)
self[key] = klass != String ? CastedArray.new(arr) : arr self[key] = klass != String ? CastedArray.new(arr) : arr
self[key].casted_by = self if self[key].respond_to?(:casted_by) self[key].casted_by = self if self[key].respond_to?(:casted_by)
else else

View file

@ -28,10 +28,9 @@ module CouchRest
def typecast_value(value, klass, init_method) def typecast_value(value, klass, init_method)
return nil if value.nil? return nil if value.nil?
if value.instance_of?(klass) || klass == Object
if value.instance_of?(klass) || klass.to_s == 'Object' value
value elsif [String, TrueClass, Integer, Float, BigDecimal, DateTime, Time, Date, Class].include?(klass)
elsif ['String', 'TrueClass', 'Integer', 'Float', 'BigDecimal', 'DateTime', 'Time', 'Date', 'Class'].include?(klass.to_s)
send('typecast_to_'+klass.to_s.downcase, value) send('typecast_to_'+klass.to_s.downcase, value)
else else
# Allow the init_method to be defined as a Proc for advanced conversion # Allow the init_method to be defined as a Proc for advanced conversion
@ -43,7 +42,7 @@ module CouchRest
# Typecast a value to an Integer # Typecast a value to an Integer
def typecast_to_integer(value) def typecast_to_integer(value)
value.kind_of?(Integer) ? value : typecast_to_numeric(value, :to_i) typecast_to_numeric(value, :to_i)
end end
# Typecast a value to a String # Typecast a value to a String
@ -65,8 +64,6 @@ module CouchRest
# Typecast a value to a BigDecimal # Typecast a value to a BigDecimal
def typecast_to_bigdecimal(value) def typecast_to_bigdecimal(value)
return value if value.kind_of?(BigDecimal)
if value.kind_of?(Integer) if value.kind_of?(Integer)
value.to_s.to_d value.to_s.to_d
else else
@ -76,7 +73,6 @@ module CouchRest
# Typecast a value to a Float # Typecast a value to a Float
def typecast_to_float(value) def typecast_to_float(value)
return value if value.kind_of?(Float)
typecast_to_numeric(value, :to_f) typecast_to_numeric(value, :to_f)
end end
@ -97,9 +93,8 @@ module CouchRest
# Typecasts an arbitrary value to a DateTime. # Typecasts an arbitrary value to a DateTime.
# Handles both Hashes and DateTime instances. # Handles both Hashes and DateTime instances.
# This is slow!! Use Time instead.
def typecast_to_datetime(value) def typecast_to_datetime(value)
return value if value.kind_of?(DateTime)
if value.is_a?(Hash) if value.is_a?(Hash)
typecast_hash_to_datetime(value) typecast_hash_to_datetime(value)
else else
@ -112,12 +107,15 @@ module CouchRest
# Typecasts an arbitrary value to a Date # Typecasts an arbitrary value to a Date
# Handles both Hashes and Date instances. # Handles both Hashes and Date instances.
def typecast_to_date(value) def typecast_to_date(value)
return value if value.kind_of?(Date)
if value.is_a?(Hash) if value.is_a?(Hash)
typecast_hash_to_date(value) typecast_hash_to_date(value)
elsif value.is_a?(Time) # sometimes people think date is time!
value.to_date
elsif value.to_s =~ /(\d{4})[\-|\/](\d{2})[\-|\/](\d{2})/
# Faster than parsing the date
Date.new($1.to_i, $2.to_i, $3.to_i)
else else
Date.parse(value.to_s) Date.parse(value)
end end
rescue ArgumentError rescue ArgumentError
value value
@ -126,8 +124,6 @@ module CouchRest
# Typecasts an arbitrary value to a Time # Typecasts an arbitrary value to a Time
# Handles both Hashes and Time instances. # Handles both Hashes and Time instances.
def typecast_to_time(value) def typecast_to_time(value)
return value if value.kind_of?(Time)
if value.is_a?(Hash) if value.is_a?(Hash)
typecast_hash_to_time(value) typecast_hash_to_time(value)
else else
@ -160,7 +156,6 @@ module CouchRest
# uses the value of Time.now. # uses the value of Time.now.
def extract_time(value) def extract_time(value)
now = Time.now now = Time.now
[:year, :month, :day, :hour, :min, :sec].map do |segment| [:year, :month, :day, :hour, :min, :sec].map do |segment|
typecast_to_numeric(value.fetch(segment, now.send(segment)), :to_i) typecast_to_numeric(value.fetch(segment, now.send(segment)), :to_i)
end end
@ -168,7 +163,6 @@ module CouchRest
# Typecast a value to a Class # Typecast a value to a Class
def typecast_to_class(value) def typecast_to_class(value)
return value if value.kind_of?(Class)
::CouchRest.constantize(value.to_s) ::CouchRest.constantize(value.to_s)
rescue NameError rescue NameError
value value