Sha256: a7a03e949994397a689f3e8f31b9e88b050e68bc7ac46775f4de21810e63c53e

Contents?: true

Size: 1.58 KB

Versions: 42

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    module Helpers # :nodoc: all
      module Numeric
        def serialize(value)
          cast(value)
        end

        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

        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

        private
          def equal_nan?(old_value, new_value)
            (old_value.is_a?(::Float) || old_value.is_a?(BigDecimal)) &&
              old_value.nan? &&
              old_value.instance_of?(new_value.class) &&
              new_value.nan?
          end

          def number_to_non_number?(old_value, new_value_before_type_cast)
            old_value != nil && non_numeric_string?(new_value_before_type_cast.to_s)
          end

          def non_numeric_string?(value)
            # 'wibble'.to_i will give zero, we want to make sure
            # that we aren't marking int zero to string zero as
            # changed.
            !NUMERIC_REGEX.match?(value)
          end

          NUMERIC_REGEX = /\A\s*[+-]?\d/
          private_constant :NUMERIC_REGEX
      end
    end
  end
end

Version data entries

42 entries across 40 versions & 5 rubygems

Version Path
activemodel-7.0.8.7 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.8.6 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.8.5 lib/active_model/type/helpers/numeric.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activemodel-7.0.8.4/lib/active_model/type/helpers/numeric.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/type/helpers/numeric.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/type/helpers/numeric.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/type/helpers/numeric.rb
activemodel-7.0.8.4 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.8.1 lib/active_model/type/helpers/numeric.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-7.0.2.3/lib/active_model/type/helpers/numeric.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-7.0.3.1/lib/active_model/type/helpers/numeric.rb
activemodel-7.0.8 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.7.2 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.7.1 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.7 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.6 lib/active_model/type/helpers/numeric.rb
activemodel-7.0.5.1 lib/active_model/type/helpers/numeric.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activemodel-7.0.3.1/lib/active_model/type/helpers/numeric.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activemodel-7.0.2.3/lib/active_model/type/helpers/numeric.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4/lib/active_model/type/helpers/numeric.rb