Module: EacCli::RunnerWith::Subcommands
- Defined in:
- lib/eac_cli/runner_with/subcommands.rb,
lib/eac_cli/runner_with/subcommands/definition_concern.rb
Defined Under Namespace
Modules: DefinitionConcern
Constant Summary
collapse
- EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME =
:extra_available_subcommands
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 59
def method_missing(method_name, *arguments, &block)
return run_with_subcommand(*arguments, &block) if
run_with_subcommand_alias_run?(method_name)
super
end
|
Class Method Details
.runner?(object) ⇒ Boolean
12
13
14
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 12
def runner?(object)
::EacCli::Runner.runner?(object)
end
|
.subcommands_from_module(a_module) ⇒ Hash<String, EacCli::Runner>
17
18
19
20
21
22
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 17
def subcommands_from_module(a_module)
a_module.constants
.map { |name| [name.to_s.underscore.gsub('_', '-'), a_module.const_get(name)] }
.select { |c| runner?(c[1]) }
.to_h.with_indifferent_access
end
|
Instance Method Details
#available_subcommands ⇒ Object
33
34
35
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 33
def available_subcommands
@available_subcommands ||= available_subcommands_auto.merge(available_subcommands_extra)
end
|
#available_subcommands_extra ⇒ Object
#available_subcommands_to_s ⇒ Object
50
51
52
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 50
def available_subcommands_to_s
available_subcommands.keys.sort.join(', ')
end
|
Returns Hash<String, Enumerable<String>].
55
56
57
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 55
def
help_list_section('Subcommands', available_subcommands.keys.sort)
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
66
67
68
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 66
def respond_to_missing?(method_name, include_private = false)
run_with_subcommand_alias_run?(method_name) || super
end
|
#run_subcommand? ⇒ Boolean
90
91
92
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 90
def run_subcommand?
subcommand_name.present?
end
|
#run_with_subcommand ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 70
def run_with_subcommand
if run_subcommand?
if subcommand_runner.respond_to?(:run_run)
subcommand_runner.run_run
else
subcommand_runner.run
end
else
run_without_subcommand
end
end
|
#run_with_subcommand_alias_run?(method_name) ⇒ Boolean
82
83
84
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 82
def run_with_subcommand_alias_run?(method_name)
subcommands? && method_name.to_sym == :run
end
|
#run_without_subcommand ⇒ Object
86
87
88
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 86
def run_without_subcommand
"Method #{__method__} should be overrided in #{self.class.name}"
end
|
#subcommand_class ⇒ Object
102
103
104
105
106
107
108
109
110
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 102
def subcommand_class
available_subcommands[subcommand_name].if_present { |v| return v }
raise(::EacCli::Parser::Error.new(
self.class.runner_definition, runner_context.argv,
"Subcommand \"#{subcommand_name}\" not found " \
"(Available: #{available_subcommands_to_s})"
))
end
|
#subcommand_program ⇒ Object
116
117
118
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 116
def subcommand_program
[program_name, subcommand_name]
end
|
#subcommand_runner ⇒ Object
120
121
122
123
124
125
126
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 120
def subcommand_runner
@subcommand_runner ||= subcommand_class.create(
argv: subcommand_args,
program_name: subcommand_program,
parent: self
)
end
|
#subcommands? ⇒ Boolean
94
95
96
|
# File 'lib/eac_cli/runner_with/subcommands.rb', line 94
def subcommands?
self.class.runner_definition.subcommands?
end
|