Sha256: e641bc1fc33115129e8d8c2874992ee7fc9c9149835ded23b2d923fcdc00ad09

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module ShallowAttributes
  module Type
    # Abstract class for typecast object to Boolean type.
    #
    # @abstract
    #
    # @since 0.1.0
    class Boolean
      # Array of true values
      #
      # @private
      #
      # @since 0.1.0
      TRUE_VALUES  = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].freeze

      # Array of false values
      #
      # @private
      #
      # @since 0.1.0
      FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', nil].freeze

      # Convert value to Boolean type
      #
      # @private
      #
      # @param [Object] value
      # @param [Hash] _options
      #
      # @example Convert integer to boolean value
      #   ShallowAttributes::Type::Boolean.new.coerce(1)
      #     # => true
      #
      #   ShallowAttributes::Type::Boolean.new.coerce(0)
      #     # => false
      #
      # @raise [InvalidValueError] if value is not included in true and false arrays
      #
      # @return [boolean]
      #
      # @since 0.1.0
      def coerce(value, _options = {})
        if TRUE_VALUES.include?(value)
          true
        elsif FALSE_VALUES.include?(value)
          false
        else
          raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Boolean")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
shallow_attributes-0.9.5 lib/shallow_attributes/type/boolean.rb
shallow_attributes-0.9.4 lib/shallow_attributes/type/boolean.rb
shallow_attributes-0.9.3 lib/shallow_attributes/type/boolean.rb