Sha256: ee91d04b10c2e88413ffaebc103906b3186af3d4d944180de48d5a8040059f89

Contents?: true

Size: 1.03 KB

Versions: 42

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    # = Active Model \Boolean \Type
    #
    # A class that behaves like a boolean type, including rules for coercion of
    # user input.
    #
    # - <tt>"false"</tt>, <tt>"f"</tt>, <tt>"0"</tt>, +0+ or any other value in
    #   +FALSE_VALUES+ will be coerced to +false+.
    # - Empty strings are coerced to +nil+.
    # - All other values will be coerced to +true+.
    class Boolean < Value
      FALSE_VALUES = [
        false, 0,
        "0", :"0",
        "f", :f,
        "F", :F,
        "false", :false,
        "FALSE", :FALSE,
        "off", :off,
        "OFF", :OFF,
      ].to_set.freeze

      def type # :nodoc:
        :boolean
      end

      def serialize(value) # :nodoc:
        cast(value)
      end

      def serialize_cast_value(value) # :nodoc:
        value
      end

      private
        def cast_value(value)
          if value == ""
            nil
          else
            !FALSE_VALUES.include?(value)
          end
        end
    end
  end
end

Version data entries

42 entries across 42 versions & 6 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activemodel-7.1.3.4/lib/active_model/type/boolean.rb
activemodel-8.0.1 lib/active_model/type/boolean.rb
activemodel-8.0.0.1 lib/active_model/type/boolean.rb
activemodel-7.2.2.1 lib/active_model/type/boolean.rb
activemodel-7.1.5.1 lib/active_model/type/boolean.rb
activemodel-8.0.0 lib/active_model/type/boolean.rb
activemodel-7.2.2 lib/active_model/type/boolean.rb
activemodel-7.1.5 lib/active_model/type/boolean.rb
activemodel-8.0.0.rc2 lib/active_model/type/boolean.rb
activemodel-7.2.1.2 lib/active_model/type/boolean.rb
activemodel-7.1.4.2 lib/active_model/type/boolean.rb
activemodel-8.0.0.rc1 lib/active_model/type/boolean.rb
activemodel-7.2.1.1 lib/active_model/type/boolean.rb
activemodel-7.1.4.1 lib/active_model/type/boolean.rb
activemodel-8.0.0.beta1 lib/active_model/type/boolean.rb
omg-activemodel-8.0.0.alpha9 lib/active_model/type/boolean.rb
omg-activemodel-8.0.0.alpha8 lib/active_model/type/boolean.rb
omg-activemodel-8.0.0.alpha7 lib/active_model/type/boolean.rb
omg-activemodel-8.0.0.alpha4 lib/active_model/type/boolean.rb
omg-activemodel-8.0.0.alpha3 lib/active_model/type/boolean.rb