Sha256: 155e9d1c3a44f1acd19fabedd2abe255fbf693e850fb14ef03cfc64566f3a7ee

Contents?: true

Size: 1.09 KB

Versions: 104

Compression:

Stored size: 1.09 KB

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", :"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

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

Version data entries

104 entries across 100 versions & 9 rubygems

Version Path
activemodel-7.0.8.6 lib/active_model/type/boolean.rb
activemodel-6.1.7.10 lib/active_model/type/boolean.rb
activemodel-6.1.7.9 lib/active_model/type/boolean.rb
activemodel-7.0.8.5 lib/active_model/type/boolean.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activemodel-7.0.8.4/lib/active_model/type/boolean.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/type/boolean.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/type/boolean.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activemodel-7.0.5.1/lib/active_model/type/boolean.rb
activemodel-7.0.8.4 lib/active_model/type/boolean.rb
activemodel-6.1.7.8 lib/active_model/type/boolean.rb
activemodel-7.0.8.1 lib/active_model/type/boolean.rb
activemodel-6.1.7.7 lib/active_model/type/boolean.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-7.0.2.3/lib/active_model/type/boolean.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-6.1.6.1/lib/active_model/type/boolean.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-7.0.3.1/lib/active_model/type/boolean.rb
activemodel-7.0.8 lib/active_model/type/boolean.rb
activemodel-7.0.7.2 lib/active_model/type/boolean.rb
activemodel-6.1.7.6 lib/active_model/type/boolean.rb
activemodel-7.0.7.1 lib/active_model/type/boolean.rb
activemodel-6.1.7.5 lib/active_model/type/boolean.rb