Class: CovLoupe::BooleanType

Inherits:
Object
  • Object
show all
Defined in:
lib/cov_loupe/config/boolean_type.rb

Overview

A pluggable boolean type converter for OptionParser. Accepts various string representations of true/false and converts them to boolean values.

Usage with OptionParser:

parser.accept(BooleanType) { |v| BooleanType.parse(v) }
parser.on('--flag [BOOLEAN]', BooleanType) { |v| config.flag = v }

Supported values (case-insensitive):

true:  yes, y, true, t, on, +, 1
false: no, n, false, f, off, -, 0
nil:   treated as true (for bare flags like --flag)

Examples:

--flag         → true
--flag=yes     → true
--flag=no      → false
--flag yes     → true
--flag false   → false

Constant Summary collapse

TRUE_VALUES =

Values that map to true

%w[yes y true t on + 1].freeze
FALSE_VALUES =

Values that map to false

%w[no n false f off - 0].freeze
VALID_VALUES =

All valid boolean string values: %w{yes no y n true false t f on off + - 1 0 }

TRUE_VALUES.zip(FALSE_VALUES).flatten.freeze
BOOLEAN_VALUES_DISPLAY_STRING =

String representation for help messages ('yes/no/true/false/t/f/on/off/y/n/+/-/1/0')

VALID_VALUES.join('/').freeze
IS_BOOLEAN_STRING_VALUE =

Pattern object for OptionParser. Proc objects get treated as blocks, and Module instances are rejected outright, so we expose a singleton that only responds to #match like a regex.

Object.new

Class Method Summary collapse

Class Method Details

.===(value) ⇒ Boolean?

Pattern matching for OptionParser (called via ===) This is called to determine if a token should be consumed as the option's argument. Returning nil signals OptionParser to NOT consume the token.

Parameters:

  • value (String, nil)

    The value to match

Returns:

  • (Boolean, nil)

    The parsed boolean if valid, or nil to reject the token



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cov_loupe/config/boolean_type.rb', line 78

def ===(value)
  # nil means optional argument not provided - accept as match
  return true if value.nil?

  # Only consume the token if it's a valid boolean value
  # This prevents consuming subcommand names or other arguments
  return true if valid?(value)

  # Invalid value - don't consume it, let OptionParser treat it as the next argument
  nil
end

.parse(value) ⇒ Boolean

Parse a string value into a boolean.

Parameters:

  • value (String, nil)

    The value to parse

Returns:

  • (Boolean)

    true or false

Raises:

  • (ArgumentError)

    if the value is not a valid boolean string



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cov_loupe/config/boolean_type.rb', line 49

def parse(value)
  # nil means bare flag (e.g., --flag without a value) → true
  return true if value.nil?

  normalized = value.to_s.strip.downcase

  return true if TRUE_VALUES.include?(normalized)
  return false if FALSE_VALUES.include?(normalized)

  raise ArgumentError, "invalid boolean value: #{value.inspect}. " \
                      "Valid values: #{VALID_VALUES.join(', ')}"
end

.valid?(value) ⇒ Boolean

Check if a value is a valid boolean string.

Parameters:

  • value (String, nil)

    The value to check

Returns:

  • (Boolean)

    true if valid, false otherwise



66
67
68
69
70
# File 'lib/cov_loupe/config/boolean_type.rb', line 66

def valid?(value)
  return true if value.nil?

  VALID_VALUES.include?(value.to_s.strip.downcase)
end