Module: DataMapper::Property::Typecast::Numeric

Included in:
Numeric, Time
Defined in:
lib/dm-core/property/typecast/numeric.rb

Instance Method Summary collapse

Instance Method Details

#typecast_to_numeric(value, method) ⇒ Numeric

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Match numeric string

Parameters:

  • value (#to_str, Numeric)

    value to typecast

  • method (Symbol)

    method to typecast with

Returns:

  • (Numeric)

    number if matched, value if no match



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dm-core/property/typecast/numeric.rb', line 16

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