Sha256: 3b4ef2cad27e86b8136971c4914d070fbb2440d1e36770e36ad245f1ec2d1a11

Contents?: true

Size: 918 Bytes

Versions: 4

Compression:

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

4 entries across 4 versions & 1 rubygems

Version Path
options_model-0.0.10 lib/active_model/type/boolean.rb
options_model-0.0.9 lib/active_model/type/boolean.rb
options_model-0.0.8 lib/active_model/type/boolean.rb
options_model-0.0.7 lib/active_model/type/boolean.rb