module CouchRest::Mixins::Typecast

Public Instance Methods

typecast_value(value, property) click to toggle source
# File lib/couchrest/mixins/typecast.rb, line 28
def typecast_value(value, property) # klass, init_method)
  return nil if value.nil?
  klass = property.type_class
  if value.instance_of?(klass) || klass == Object
    value
  elsif [String, TrueClass, Integer, Float, BigDecimal, DateTime, Time, Date, Class].include?(klass)
    send('typecast_to_'+klass.to_s.downcase, value)
  else
    # Allow the init_method to be defined as a Proc for advanced conversion
    property.init_method.is_a?(Proc) ? property.init_method.call(value) : klass.send(property.init_method, value)
  end
end

Protected Instance Methods

extract_time(value) click to toggle source

Extracts the given args from the hash. If a value does not exist, it uses the value of Time.now.

# File lib/couchrest/mixins/typecast.rb, line 157
def extract_time(value)
  now  = Time.now
  [:year, :month, :day, :hour, :min, :sec].map do |segment|
    typecast_to_numeric(value.fetch(segment, now.send(segment)), :to_i)
  end
end
typecast_hash_to_date(value) click to toggle source

Creates a Date instance from a Hash with keys :year, :month, :day

# File lib/couchrest/mixins/typecast.rb, line 145
def typecast_hash_to_date(value)
  Date.new(*extract_time(value)[0, 3])
end
typecast_hash_to_datetime(value) click to toggle source

Creates a DateTime instance from a Hash with keys :year, :month, :day, :hour, :min, :sec

# File lib/couchrest/mixins/typecast.rb, line 140
def typecast_hash_to_datetime(value)
  DateTime.new(*extract_time(value))
end
typecast_hash_to_time(value) click to toggle source

Creates a Time instance from a Hash with keys :year, :month, :day, :hour, :min, :sec

# File lib/couchrest/mixins/typecast.rb, line 151
def typecast_hash_to_time(value)
  Time.local(*extract_time(value))
end
typecast_to_bigdecimal(value) click to toggle source

Typecast a value to a BigDecimal

# File lib/couchrest/mixins/typecast.rb, line 66
def typecast_to_bigdecimal(value)
  if value.kind_of?(Integer)
    value.to_s.to_d
  else
    typecast_to_numeric(value, :to_d)
  end
end
typecast_to_class(value) click to toggle source

Typecast a value to a Class

# File lib/couchrest/mixins/typecast.rb, line 165
def typecast_to_class(value)
  value.to_s.constantize
rescue NameError
  value
end
typecast_to_date(value) click to toggle source

Typecasts an arbitrary value to a Date Handles both Hashes and Date instances.

# File lib/couchrest/mixins/typecast.rb, line 109
def typecast_to_date(value)
  if value.is_a?(Hash)
    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
    Date.parse(value)
  end
rescue ArgumentError
  value
end
typecast_to_datetime(value) click to toggle source

Typecasts an arbitrary value to a DateTime. Handles both Hashes and DateTime instances. This is slow!! Use Time instead.

# File lib/couchrest/mixins/typecast.rb, line 97
def typecast_to_datetime(value)
  if value.is_a?(Hash)
    typecast_hash_to_datetime(value)
  else
    DateTime.parse(value.to_s)
  end
rescue ArgumentError
  value
end
typecast_to_float(value) click to toggle source

Typecast a value to a Float

# File lib/couchrest/mixins/typecast.rb, line 75
def typecast_to_float(value)
  typecast_to_numeric(value, :to_f)
end
typecast_to_integer(value) click to toggle source

Typecast a value to an Integer

# File lib/couchrest/mixins/typecast.rb, line 44
def typecast_to_integer(value)
  typecast_to_numeric(value, :to_i)
end
typecast_to_numeric(value, method) click to toggle source

Match numeric string

# File lib/couchrest/mixins/typecast.rb, line 80
def typecast_to_numeric(value, method)
  if value.respond_to?(:to_str)
    if value.to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/
      $1.send(method)
    else
      value
    end
  elsif value.respond_to?(method)
    value.send(method)
  else
    value
  end
end
typecast_to_string(value) click to toggle source

Typecast a value to a String

# File lib/couchrest/mixins/typecast.rb, line 49
def typecast_to_string(value)
  value.to_s
end
typecast_to_time(value) click to toggle source

Typecasts an arbitrary value to a Time Handles both Hashes and Time instances.

# File lib/couchrest/mixins/typecast.rb, line 126
def typecast_to_time(value)
  if value.is_a?(Hash)
    typecast_hash_to_time(value)
  else
    Time.mktime_with_offset(value.to_s)
  end
rescue ArgumentError
  value
rescue TypeError
  value
end
typecast_to_trueclass(value) click to toggle source

Typecast a value to a true or false

# File lib/couchrest/mixins/typecast.rb, line 54
def typecast_to_trueclass(value)
  if value.kind_of?(Integer)
    return true  if value == 1
    return false if value == 0
  elsif value.respond_to?(:to_s)
    return true  if %w[ true  1 t ].include?(value.to_s.downcase)
    return false if %w[ false 0 f ].include?(value.to_s.downcase)
  end
  value
end