Sha256: 4b01b3d050ab9eaf80aff604ff0cfa8a5bcad6aa37a2efcbff23b05c3d97cd68

Contents?: true

Size: 899 Bytes

Versions: 7

Compression:

Stored size: 899 Bytes

Contents

# frozen_string_literal: true

module Acfs::Resource::Attributes
  # @api public
  #
  # Boolean attribute type. Use it in your model as an attribute type:
  #
  # @example
  #   class User < Acfs::Resource
  #     attribute :name, :boolean
  #   end
  #
  # Given objects will be converted to string. The following strings
  # are considered true, everything else false:
  #
  #  true, on, yes
  #
  class Boolean < Base
    FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO'].to_set

    # @api public
    #
    # Cast given object to boolean.
    #
    # @param [Object] value Object to cast.
    # @return [TrueClass, FalseClass] Casted boolean.
    #
    def cast_value(value)
      return true if value == true
      return false if value == false

      if value.blank?
        nil
      else
        !FALSE_VALUES.include?(value)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
acfs-2.0.0 lib/acfs/resource/attributes/boolean.rb
acfs-1.7.0 lib/acfs/resource/attributes/boolean.rb
acfs-1.6.0 lib/acfs/resource/attributes/boolean.rb
acfs-1.5.1 lib/acfs/resource/attributes/boolean.rb
acfs-1.5.0 lib/acfs/resource/attributes/boolean.rb
acfs-1.4.0 lib/acfs/resource/attributes/boolean.rb
acfs-1.3.4 lib/acfs/resource/attributes/boolean.rb