Module: RatatuiRuby::Widgets::CoerceableWidget::ClassMethods

Defined in:
lib/ratatui_ruby/widgets/coerceable_widget.rb

Overview

Class methods extended onto widget classes.

Instance Method Summary collapse

Instance Method Details

#coerce_args(first, kwargs) ⇒ Object

Coerces a bare Hash argument into keyword arguments.

Parameters:

  • first (Hash, nil)

    First positional argument (bare hash case)

  • kwargs (Hash)

    Keyword arguments (normal splatted case)

Returns:

  • (Object)

    New instance of the widget class

Raises:

  • (ArgumentError)

    In debug mode, if unknown keys are present



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ratatui_ruby/widgets/coerceable_widget.rb', line 63

def coerce_args(first, kwargs)
  if first.is_a?(Hash) && kwargs.empty?
    unknown = first.keys - self::KNOWN_KEYS
    if unknown.any? && RatatuiRuby::Debug.enabled?
      raise ArgumentError, "#{name}: unknown keys #{unknown.inspect}"
    end
    new(**first.slice(*self::KNOWN_KEYS))
  else
    new(**kwargs)
  end
end