Sha256: 10d4358f23086c097f0605f8355e930d945233c8de2750d1a819fb3e34b6e8c6

Contents?: true

Size: 1000 Bytes

Versions: 16

Compression:

Stored size: 1000 Bytes

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    # == Active \Model \Type \Boolean
    #
    # A class that behaves like a boolean type, including rules for coercion of user input.
    #
    # === Coercion
    # Values set from user input will first be coerced into the appropriate ruby type.
    # Coercion behavior is roughly mapped to Ruby's boolean semantics.
    #
    # - "false", "f" , "0", +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", "f", "F", "false", "FALSE", "off", "OFF"].to_set

      def type # :nodoc:
        :boolean
      end

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

      private

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

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
spiral_form-0.1.1 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/type/boolean.rb
spiral_form-0.1.0 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/type/boolean.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/activemodel-5.2.3/lib/active_model/type/boolean.rb
ric-0.12.2 vendor/bundle/ruby/2.5.0/gems/activemodel-5.2.3/lib/active_model/type/boolean.rb
activemodel-5.2.3 lib/active_model/type/boolean.rb
activemodel-5.2.3.rc1 lib/active_model/type/boolean.rb
activemodel-6.0.0.beta3 lib/active_model/type/boolean.rb
activemodel-5.2.2.1 lib/active_model/type/boolean.rb
activemodel-6.0.0.beta2 lib/active_model/type/boolean.rb
activemodel-6.0.0.beta1 lib/active_model/type/boolean.rb
nullifyable-0.1.0 vendor/bundle/gems/activemodel-5.2.2/lib/active_model/type/boolean.rb
activemodel-5.2.2 lib/active_model/type/boolean.rb
activemodel-5.2.2.rc1 lib/active_model/type/boolean.rb
activemodel-5.2.1.1 lib/active_model/type/boolean.rb
activemodel-5.2.1 lib/active_model/type/boolean.rb
activemodel-5.2.1.rc1 lib/active_model/type/boolean.rb