Sha256: 24263b12718e2fc4ef8c5d59ee5805c1726cf0fda809ac984cdd89eb67e6aea4

Contents?: true

Size: 1006 Bytes

Versions: 9

Compression:

Stored size: 1006 Bytes

Contents

# frozen_string_literal: true

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

9 entries across 9 versions & 2 rubygems

Version Path
activemodel-5.2.1.1 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.1 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.1.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.0 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.0.rc2 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.0.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.0.beta2 lib/active_model/type/helpers/numeric.rb
activemodel-5.2.0.beta1 lib/active_model/type/helpers/numeric.rb
ruby-on-quails-0.1.0 activemodel/lib/active_model/type/helpers/numeric.rb