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



266
267
268
269
270
271
272
# File 'lib/arel/algebra/attributes/attribute.rb', line 266

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



274
275
276
277
278
279
280
281
282
283
284
# File 'lib/arel/algebra/attributes/attribute.rb', line 274

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:



286
287
288
# File 'lib/arel/algebra/attributes/attribute.rb', line 286

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