Module: TIntMe::Style::ConstructorExtensions Private

Included in:
TIntMe::Style
Defined in:
lib/tint_me/style.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Module to extend constructor with positional argument support

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override [] to support positional arguments



99
100
101
102
103
# File 'lib/tint_me/style.rb', line 99

def [](*args, **)
  return super(**) if args.empty?

  super(**normalize_constructor_args(*args, **))
end

#new(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override new to support positional arguments



92
93
94
95
96
# File 'lib/tint_me/style.rb', line 92

def new(*args, **)
  return super(**) if args.empty?

  super(**normalize_constructor_args(*args, **))
end

#normalize_constructor_args(*args, **kwargs) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalize positional and keyword arguments for Style construction

Parameters:

  • args (Array)

    Positional arguments (colors and boolean flags)

  • kwargs (Hash)

    Keyword arguments

Returns:

  • (Hash)

    Normalized keyword arguments



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tint_me/style.rb', line 53

def normalize_constructor_args(*args, **kwargs)
  # Validate all positional arguments at once using array validation
  Types::PositionalArgumentsArray[args] unless args.empty?

  # Now normalize the positional arguments
  normalized = kwargs.dup
  colors = []

  args.each do |arg|
    # Determine if it's a color or a boolean flag
    if Types::ColorSymbol.valid?(arg) || Types::ColorString.valid?(arg)
      # Color argument - collect up to 2 colors
      colors << arg
    elsif Types::BooleanFlagSymbol.valid?(arg)
      # Boolean flag - set to true (idempotent)
      normalized[arg] = true unless normalized.key?(arg)
    end
  end

  # Validate maximum 2 colors
  if colors.length > 2
    raise ArgumentError, "Too many color arguments (maximum 2 allowed, got #{colors.length})"
  end

  # Apply colors if specified (keyword arguments take precedence)
  if colors.length >= 1 && !normalized.key?(:foreground)
    normalized[:foreground] = colors[0]
  end
  if colors.length == 2 && !normalized.key?(:background)
    normalized[:background] = colors[1]
  end

  normalized
rescue Dry::Types::CoercionError => e
  # Extract the invalid argument from the error for a cleaner message
  raise ArgumentError, "Invalid positional argument(s): #{e.message}"
end