Sha256: cf8ada1c08635d5092e01255ee6bff7a42080db9bc218406514d8badc0aba138

Contents?: true

Size: 975 Bytes

Versions: 22

Compression:

Stored size: 975 Bytes

Contents

module ActiveModel
  module Type
    module Helpers # :nodoc: all
      module Numeric
        def cast(value)
          value = \
            case value
            when true then 1
            when false then 0
            when ::String then value.presence
            else value
            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)
        end

        private

          def number_to_non_number?(old_value, new_value_before_type_cast)
            old_value != nil && non_numeric_string?(new_value_before_type_cast)
          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.
            value.to_s !~ /\A-?\d+\.?\d*\z/
          end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
activemodel-5.1.7 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.7.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.6.2 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.6.1 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.6 lib/active_model/type/helpers/numeric.rb
tdiary-5.0.8 vendor/bundle/gems/activemodel-5.1.5/lib/active_model/type/helpers/numeric.rb
activemodel-5.1.5 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.5.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.4 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.4.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.3 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.3.rc3 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.3.rc2 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.3.rc1 lib/active_model/type/helpers/numeric.rb
tdiary-5.0.5 vendor/bundle/gems/activemodel-5.1.2/lib/active_model/type/helpers/numeric.rb
activemodel-5.1.2 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.2.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.1 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.0 lib/active_model/type/helpers/numeric.rb
activemodel-5.1.0.rc2 lib/active_model/type/helpers/numeric.rb