Class: Eco::CLI::Config::UseCases
- Inherits:
-
Object
- Object
- Eco::CLI::Config::UseCases
- Includes:
- Help
- Defined in:
- lib/eco/cli/config/use_cases.rb
Instance Attribute Summary collapse
-
#core_config ⇒ Object
readonly
Returns the value of attribute core_config.
Instance Method Summary collapse
-
#active(io:) ⇒ Hash
Scopes/identifies which usecases are being invoked from the command line.
-
#add(option_case, type, desc = nil, case_name: nil) ⇒ Object
Integrates a usecase to the command line.
-
#help ⇒ String
Summary of the use cases.
-
#initialize(core_config:) ⇒ UseCases
constructor
A new instance of UseCases.
- #process(io:) ⇒ Object
Constructor Details
#initialize(core_config:) ⇒ UseCases
Returns a new instance of UseCases.
8 9 10 11 12 |
# File 'lib/eco/cli/config/use_cases.rb', line 8 def initialize(core_config:) @core_config = core_config @linked_cases = {} @description = {} end |
Instance Attribute Details
#core_config ⇒ Object (readonly)
Returns the value of attribute core_config.
6 7 8 |
# File 'lib/eco/cli/config/use_cases.rb', line 6 def core_config @core_config end |
Instance Method Details
#active(io:) ⇒ Hash
Note:
- this method will sort the active usecases by the position they hold in the command line
Scopes/identifies which usecases are being invoked from the command line
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/eco/cli/config/use_cases.rb', line 55 def active(io:) validate_io!(io) return @active_cases unless !@active_cases active_cases = {} @linked_cases.each do |option_case, types| next nil unless SCR.get_arg(option_case) types.each do |type, data| if usecase = get_usecase(io: io, data: data) active_cases[usecase] = { index: SCR.get_arg_index(option_case), option: option_case, callback: data[:callback] } end end end @active_cases = active_cases.sort_by {|c, d| d[:index]}.to_h end |
#add(option_case, type, desc = nil, case_name: nil) ⇒ Object
Integrates a usecase to the command line.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/eco/cli/config/use_cases.rb', line 30 def add(option_case, type, desc = nil, case_name: nil) Eco::API::UseCases::UseCase.validate_type(type) unless callback = block_given?? Proc.new : nil raise "You must specify a valid 'case_name' when no block is provided" unless case_name raise "'case_name' expected to be a String. Given: #{case_name.class}" unless case_name.is_a?(String) end @linked_cases[option_case] = { type => { option: option_case, type: type, casename: case_name, callback: callback } } @description[option_case] = desc self end |
#help ⇒ String
Returns summary of the use cases.
15 16 17 18 19 20 21 22 23 |
# File 'lib/eco/cli/config/use_cases.rb', line 15 def help ["The following are the available use cases:"].yield_self do |lines| max_len = keys_max_len(@linked_cases.keys) @linked_cases.keys.sort.each do |key| lines << help_line(key, @description[key], max_len) end lines end.join("\n") end |
#process(io:) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/eco/cli/config/use_cases.rb', line 74 def process(io:) validate_io!(io) processed = false active(io: io).each do |usecase, data| raise "Something went wrong when scoping active cases" unless data processed = true io = case_io(io: io, usecase: usecase) # some usecases have a callback to collect the parameters data[:callback]&.call(*io.params) io = usecase.launch(io: io) end processed end |