lib/eco/cli/config/use_cases.rb in eco-helpers-2.7.16 vs lib/eco/cli/config/use_cases.rb in eco-helpers-2.7.17

- old
+ new

@@ -37,31 +37,43 @@ end lines end.join("\n") end + # The linked cases + def available(keys: false) + @linked_cases.values.then do |cases| + next cases unless keys + + cases.map(&:option) + end + end + # Integrates a usecase to the command line. # @param option_case [String] the command line option to invoke the usecase. # @param type [Symbol] the type of usecase. # @param desc [String] description of the case. # @param case_name [String, nil] the name of the usecase as defined. def add(option_case, type, desc = nil, case_name: nil, &callback) Eco::API::UseCases::UseCase.validate_type(type) + unless block_given? 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 CLI case '#{option_case}'" if @linked_cases.key?(option_case) @linked_cases[option_case] = CaseConfig.new(self, option_case, type, desc, case_name, callback) end # This method runs on use cases that have lazy configurations # @note it only applies to use cases that have been defined via class def cli_apply(io:) io.session.usecases.each do |usecase| next unless usecase.respond_to?(:classed_definition) next unless (original_case = usecase.classed_definition) + original_case.cli_apply! end end # Scopes/identifies which usecases are being invoked from the command line @@ -82,13 +94,16 @@ end.sort_by {|_c, d| d.index}.to_h end 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) @@ -99,10 +114,11 @@ private # Gets a `UseCaseIO` def case_io(io:, usecase:) validate_io!(io) + case io when Eco::API::UseCases::UseCaseIO io.chain(usecase: usecase) when Eco::API::UseCases::BaseIO params = io.params(keyed: true, all: true).merge(usecase: usecase) @@ -120,15 +136,15 @@ if (callback = data.callback) # identify/retrieve usecase via callback params = io.params(keyed: true).merge(type: data.type) io = io.new(**params, validate: false) callback.call(*io.params).tap do |use| - unless use.is_a?(Eco::API::UseCases::UseCase) - msg = "When adding a usecase, without specifying 'case_name:', " - msg += "the block that integrates usecase for cli option '#{data.option}'" - msg += " must return an Eco::API::UseCases::UseCase object. It returns #{use.class}" - raise msg - end + next if use.is_a?(Eco::API::UseCases::UseCase) + + msg = "When adding a usecase, without specifying 'case_name:', " + msg << "the block that integrates usecase for cli option '#{data.option}'" + msg << "must return an Eco::API::UseCases::UseCase object. It returns #{use.class}" + raise msg end end end def validate_io!(io)