module ActiveAttr::Typecasting

Typecasting provides methods to typecast a value to a different type

The following types are supported for typecasting:

@since 0.5.0

Constants

TYPECASTER_MAP

@private

Public Instance Methods

typecast_attribute(typecaster, value) click to toggle source

Typecasts a value using a Class

@param [#call] typecaster The typecaster to use for typecasting @param [Object] value The value to be typecasted

@return [Object, nil] The typecasted value or nil if it cannot be

typecasted

@since 0.5.0

# File lib/active_attr/typecasting.rb, line 48
def typecast_attribute(typecaster, value)
  raise ArgumentError, "a typecaster must be given" unless typecaster.respond_to?(:call)
  return value if value.nil?
  typecaster.call(value)
end
typecaster_for(type) click to toggle source

Resolve a Class to a typecaster

@param [Class] type The type to cast to

@return [#call, nil] The typecaster to use

@since 0.6.0

# File lib/active_attr/typecasting.rb, line 61
def typecaster_for(type)
  typecaster = TYPECASTER_MAP[type]
  typecaster.new if typecaster
end