Class: Eco::CLI::Config::UseCases
- Inherits:
-
Object
- Object
- Eco::CLI::Config::UseCases
- Includes:
- Help
- Defined in:
- lib/eco/cli/config/use_cases.rb
Defined Under Namespace
Classes: ActiveCase, CaseConfig
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(refine: nil) ⇒ 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.
26 27 28 29 |
# File 'lib/eco/cli/config/use_cases.rb', line 26 def initialize(core_config:) @core_config = core_config @linked_cases = {} 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
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/eco/cli/config/use_cases.rb', line 65 def active(io:) validate_io!(io) return @active_cases unless !@active_cases @active_cases = @linked_cases.each_with_object({}) do |(option_case, data), active_cases| next nil unless SCR.get_arg(option_case) if usecase = get_usecase(io: io, data: data) index = SCR.get_arg_index(option_case) active_cases[usecase] = ActiveCase.new(index, option_case, data.callback) end end.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.
50 51 52 53 54 55 56 57 58 |
# File 'lib/eco/cli/config/use_cases.rb', line 50 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 puts "Overriding case config '#{option_case}'" if @linked_cases.key?(option_case) @linked_cases[option_case] = CaseConfig.new(self, option_case, type, desc, case_name, callback) end |
#help(refine: nil) ⇒ String
Returns summary of the use cases.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/eco/cli/config/use_cases.rb', line 32 def help(refine: nil) refinement = refine.is_a?(String)? " (containing: '#{refine}')" : "" ["The following are the available use cases#{refinement}:"].yield_self do |lines| max_len = keys_max_len(@linked_cases.keys) @linked_cases.keys.sort.select do |key| !refine.is_a?(String) || key.include?(refine) end.each do |option_case| lines << help_line(option_case, @linked_cases[option_case].description, max_len) end lines end.join("\n") end |
#process(io:) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/eco/cli/config/use_cases.rb', line 77 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 |