Class: Ukiryu::Models::Components

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/ukiryu/models/components.rb

Overview

Components register for reusable definitions

Enables sharing common option/argument/flag/exit_codes definitions across commands through ‘$ref` references.

Examples:

components = Components.new(
  options: { 'verbose' => OptionDefinition.new(...) },
  flags: { 'help' => FlagDefinition.new(...) }
)

Instance Method Summary collapse

Instance Method Details

#argument(name) ⇒ ArgumentDefinition?

Get an argument by name

Parameters:

  • name (String, Symbol)

    the argument name

Returns:



48
49
50
# File 'lib/ukiryu/models/components.rb', line 48

def argument(name)
  @arguments&.dig(name.to_s)
end

#can_resolve?(ref) ⇒ Boolean

Check if a reference can be resolved

Parameters:

  • ref (String)

    the reference path (e.g., ‘#/components/options/verbose’)

Returns:

  • (Boolean)

    true if the reference can be resolved



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ukiryu/models/components.rb', line 56

def can_resolve?(ref)
  return false unless ref =~ %r{^#/components/(options|flags|arguments|exit_codes)/(.+)$}

  type = Regexp.last_match(1)
  name = Regexp.last_match(2)

  case type
  when 'options'
    @options&.key?(name)
  when 'flags'
    @flags&.key?(name)
  when 'arguments'
    @arguments&.key?(name)
  when 'exit_codes'
    !@exit_codes.nil?
  else
    false
  end
end

#flag(name) ⇒ FlagDefinition?

Get a flag by name

Parameters:

  • name (String, Symbol)

    the flag name

Returns:



40
41
42
# File 'lib/ukiryu/models/components.rb', line 40

def flag(name)
  @flags&.dig(name.to_s)
end

#option(name) ⇒ OptionDefinition?

Get an option by name

Parameters:

  • name (String, Symbol)

    the option name

Returns:



32
33
34
# File 'lib/ukiryu/models/components.rb', line 32

def option(name)
  @options&.dig(name.to_s)
end

#resolve(ref) ⇒ Object?

Resolve a reference path to a component

Parameters:

  • ref (String)

    the reference path (e.g., ‘#/components/options/verbose’)

Returns:

  • (Object, nil)

    the component or nil if not found



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ukiryu/models/components.rb', line 80

def resolve(ref)
  return nil unless ref =~ %r{^#/components/(options|flags|arguments|exit_codes)/(.+)$}

  type = Regexp.last_match(1)
  name = Regexp.last_match(2)

  case type
  when 'options'
    option(name)
  when 'flags'
    flag(name)
  when 'arguments'
    argument(name)
  when 'exit_codes'
    @exit_codes
  end
end