Module: Arel::Attribute::Types

Included in:
Arel::Attribute
Defined in:
lib/arel/algebra/attributes/attribute.rb

Instance Method Summary collapse

Instance Method Details

#type_cast(value) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/arel/algebra/attributes/attribute.rb', line 244

def type_cast(value)
  if root == self
    raise NotImplementedError, "#type_cast should be implemented in a subclass."
  else
    root.type_cast(value)
  end
end

#type_cast_to_numeric(value, method) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/arel/algebra/attributes/attribute.rb', line 252

def type_cast_to_numeric(value, method)
  return unless value
  if value.respond_to?(:to_str)
    str = value.to_str.strip
    return if str.empty?
    return $1.send(method) if str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/
  elsif value.respond_to?(method)
    return value.send(method)
  end
  raise typecast_error(value)
end

#typecast_error(value) ⇒ Object

Raises:



264
265
266
# File 'lib/arel/algebra/attributes/attribute.rb', line 264

def typecast_error(value)
  raise TypecastError, "could not typecast #{value.inspect} to #{self.class.name.split('::').last}"
end