Module: ActiveModel::Type::Helpers::Numeric

Included in:
Decimal, Float, Integer
Defined in:
lib/active_model/type/helpers/numeric.rb

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_model/type/helpers/numeric.rb', line 15

def cast(value)
  # Checks whether the value is numeric. Spaceship operator
  # will return nil if value is not numeric.
  value = if value <=> 0
    value
  else
    case value
    when true then 1
    when false then 0
    else value.presence
    end
  end

  super(value)
end

#changed?(old_value, _new_value, new_value_before_type_cast) ⇒ Boolean

:nodoc:

Returns:



31
32
33
34
# File 'lib/active_model/type/helpers/numeric.rb', line 31

def changed?(old_value, _new_value, new_value_before_type_cast) # :nodoc:
  (super || number_to_non_number?(old_value, new_value_before_type_cast)) &&
    !equal_nan?(old_value, new_value_before_type_cast)
end

#serialize(value) ⇒ Object



7
8
9
# File 'lib/active_model/type/helpers/numeric.rb', line 7

def serialize(value)
  cast(value)
end

#serialize_cast_value(value) ⇒ Object



11
12
13
# File 'lib/active_model/type/helpers/numeric.rb', line 11

def serialize_cast_value(value)
  value
end