Sha256: 72bb45627d29efb7fcc27157421b40d4dd3ee532d3447cb43ff77a0a47844950

Contents?: true

Size: 932 Bytes

Versions: 6

Compression:

Stored size: 932 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

      private

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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
activemodel-5.2.0 lib/active_model/type/boolean.rb
activemodel-5.2.0.rc2 lib/active_model/type/boolean.rb
activemodel-5.2.0.rc1 lib/active_model/type/boolean.rb
activemodel-5.2.0.beta2 lib/active_model/type/boolean.rb
activemodel-5.2.0.beta1 lib/active_model/type/boolean.rb
ruby-on-quails-0.1.0 activemodel/lib/active_model/type/boolean.rb