Sha256: 1bd15ad5cfec8ae9896673d4cd46f0c4748f4472527f2b260f358772bed69eca

Contents?: true

Size: 969 Bytes

Versions: 38

Compression:

Stored size: 969 Bytes

Contents

module ActiveModel
  module Type
    module Helpers
      module Numeric # :nodoc:
        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

38 entries across 37 versions & 5 rubygems

Version Path
activemodel-5.0.7.2 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.7.1 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.7 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.6 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.6.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.5 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.5.rc2 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.5.rc1 lib/active_model/type/helpers/numeric.rb
tdiary-5.0.5 vendor/bundle/gems/activemodel-5.0.2/lib/active_model/type/helpers/numeric.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activemodel-5.0.2/lib/active_model/type/helpers/numeric.rb
activemodel-5.0.4 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.4.rc1 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.3 lib/active_model/type/helpers/numeric.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activemodel-5.0.2/lib/active_model/type/helpers/numeric.rb
tdiary-5.0.4 vendor/bundle/gems/activemodel-5.0.2/lib/active_model/type/helpers/numeric.rb
activemodel-5.0.2 lib/active_model/type/helpers/numeric.rb
activemodel-5.0.2.rc1 lib/active_model/type/helpers/numeric.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/type/helpers/numeric.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/type/helpers/numeric.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.1/lib/active_model/type/helpers/numeric.rb