module Eco class CLI class Config class UseCases attr_reader :core_config def initialize(core_config:) @core_config = core_config @linked_cases = {} end def add(option_case, type, 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 => { casename: case_name, callback: callback } } self end def process(io:) unless io && io.is_a?(Eco::API::UseCases::BaseIO) raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}" end processed = false @linked_cases.each do |option, types| types.each do |type, data| if SCR.get_arg(option) processed = true io = solve_io(io: io, type: type, option: option) usecase = nil if case_name = data[:casename] usecase = io.session.usecases.case(case_name, type: type) end if callback = data[:callback] if usecase callback.call(*io.params) else usecase = callback.call(*io.params) 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 '#{option}'" msg += " must return an Eco::API::UseCases::UseCase object. It returns #{usecase.class}" raise msg end end end usecase.launch(io: io) end end end processed end private def solve_io(io:, type:, option:) params = io.params(keyed: true).merge(type: type) if (!io.people || io.people.empty?) && io.class.people_required?(type) params.merge!(people: core_config.people) end if (!io.input || io.input.empty?) && io.class.input_required?(type) input = core_config.input.get(io: io, option: option) params.merge!(input: input) end io.new(**params) end end end end end