Sha256: 7acfcb1c9e8b4e44d6d8c4efd0bf027088d0943aed1d33fae5a4ac29ac1c0e4d

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8

module TTY
  module Conversion
    class BooleanConverter

      attr_reader :target

      attr_reader :source

      def initialize(source = String)
        @source = source
        @target = [TrueClass, FalseClass]
      end

      def boolean?(value)
        target.any? { |klass| value.is_a?(klass) }
      end

      # Convert value to boolean type including range of strings such as
      #
      # @param [Object] value
      #
      # @example
      #   coerce("True") # => true
      #
      #   other values coerced to true are:
      #     1, t, T, TRUE,  true,  True,  y, Y, YES, yes, Yes
      #
      # @example
      #   coerce("False") # => false
      #
      #  other values coerced to false are:
      #    0, f, F, FALSE, false, False, n, N, No,  no,  No
      #
      # @api public
      def convert(value)
        case value.to_s
        when /^(yes|y|t(rue)?|1)$/i
          return true
        when /^(no|n|f(alse)?|0)$/i
          return false
        else
          fail TypeError, "Expected boolean type, got #{value}"
        end
      end
    end # BooleanConverter
  end # Conversion
end # TTY

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tty-0.1.1 lib/tty/conversion/converter/boolean.rb