Sha256: da59272e448e89157adbea0288cda9a770d2059ca8ee21c0ed69b2da38ff6137

Contents?: true

Size: 1.27 KB

Versions: 41

Compression:

Stored size: 1.27 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)
        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.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

41 entries across 41 versions & 6 rubygems

Version Path
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.4.4/lib/active_model/type/helpers/numeric.rb
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.4.4/lib/active_model/type/helpers/numeric.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.4.4/lib/active_model/type/helpers/numeric.rb
activemodel-6.1.4.4 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.4.3 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.4.2 lib/active_model/type/helpers/numeric.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activemodel-6.1.4.1/lib/active_model/type/helpers/numeric.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activemodel-6.1.4.1/lib/active_model/type/helpers/numeric.rb
activemodel-6.1.4.1 lib/active_model/type/helpers/numeric.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activemodel-6.1.4/lib/active_model/type/helpers/numeric.rb
activemodel-6.1.4 lib/active_model/type/helpers/numeric.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/activemodel-6.1.3.1/lib/active_model/type/helpers/numeric.rb
activemodel-6.1.3.2 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.3.1 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.3 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.2.1 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.2 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.1 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.0 lib/active_model/type/helpers/numeric.rb
activemodel-6.1.0.rc2 lib/active_model/type/helpers/numeric.rb