lib/eco/cli/config/use_cases.rb in eco-helpers-2.5.10 vs lib/eco/cli/config/use_cases.rb in eco-helpers-2.6.0

- old
+ new

@@ -4,11 +4,10 @@ class UseCases include Eco::CLI::Config::Help attr_reader :core_config class CaseConfig < Struct.new(:cases_config, :option, :type, :description, :casename, :callback) - def add_option(arg, desc = nil, &block) core_config.options_set.add(arg, desc, namespace: option, &block) self end @@ -18,11 +17,10 @@ cases_config.core_config end end class ActiveCase < Struct.new(:index, :option, :callback) - end def initialize(core_config:) @core_config = core_config @linked_cases = {} @@ -45,20 +43,30 @@ # 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) + def add(option_case, type, desc = nil, case_name: nil, &callback) Eco::API::UseCases::UseCase.validate_type(type) - unless callback = block_given?? Proc.new : nil + 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 case config '#{option_case}'" if @linked_cases.key?(option_case) + 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 # @note # - this method will sort the active usecases by the position they hold in the command line # @param io [Eco::API::UseCases::BaseIO] the input/output object # @return [Hash] where keys are cases and values a `Hash` with `option` String and `callback` @@ -124,10 +132,9 @@ def validate_io!(io) unless io && io.is_a?(Eco::API::UseCases::BaseIO) raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}" end end - end end end end