lib/eco/cli/config/use_cases.rb in eco-helpers-2.6.4 vs lib/eco/cli/config/use_cases.rb in eco-helpers-2.7.0

- old
+ new

@@ -3,11 +3,11 @@ class Config class UseCases include Eco::CLI::Config::Help attr_reader :core_config - class CaseConfig < Struct.new(:cases_config, :option, :type, :description, :casename, :callback) + CaseConfig = Struct.new(:cases_config, :option, :type, :description, :casename, :callback) do def add_option(arg, desc = nil, &block) core_config.options_set.add(arg, desc, namespace: option, &block) self end @@ -16,22 +16,21 @@ def core_config cases_config.core_config end end - class ActiveCase < Struct.new(:index, :option, :callback) - end + ActiveCase = Struct.new(:index, :option, :callback) def initialize(core_config:) @core_config = core_config @linked_cases = {} end # @return [String] summary of the use cases. def help(refine: nil) refinement = refine.is_a?(String)? " (containing: '#{refine}')" : "" - ["The following are the available use cases#{refinement}:"].yield_self do |lines| + ["The following are the available use cases#{refinement}:"].then 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) @@ -58,11 +57,11 @@ # 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 + next unless (original_case = usecase.classed_definition) original_case.cli_apply! end end # Scopes/identifies which usecases are being invoked from the command line @@ -70,18 +69,19 @@ # - 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` def active(io:) validate_io!(io) - return @active_cases unless !@active_cases + return @active_cases if @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 + next unless SCR.get_arg(option_case) + next unless (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.sort_by {|_c, d| d.index}.to_h end def process(io:) validate_io!(io) processed = false @@ -109,31 +109,33 @@ Eco::API::UseCases::UseCaseIO.new(**params) end end def get_usecase(io:, data:) - usecase = if case_name = data.casename - io.session.usecases.case(case_name, type: data.type) - end - usecase ||= 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 |usecase| - unless usecase.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 #{usecase.class}" - raise msg + usecase = + if (case_name = data.casename) + io.session.usecases.case(case_name, type: data.type) + end + + usecase || + 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 end end - end end 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 + return if io.is_a?(Eco::API::UseCases::BaseIO) + raise ArgumentError, "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}" end end end end end