lib/eco/api/common/loaders/use_case.rb in eco-helpers-2.5.10 vs lib/eco/api/common/loaders/use_case.rb in eco-helpers-2.6.0

- old
+ new

@@ -9,13 +9,35 @@ unless value return @type || raise("You should specify a type of case [:sync, :transform, :import, :other] for #{self}") end @type = value end + + def cli! + cli&.apply! + end + + def cli(cli_class = nil) + if cli_class.is_a?(Class) + raise ArgumentError, "cli_class should inherit from Eco::API::UseCases::Cli" unless cli_class < Eco::API::UseCases::Cli + @cli = cli_class + elsif cli_class.nil? + return @cli if instance_variable_defined?(:@cli) && !@cli.nil? + # try to see if it's namespaced after the use case it provisions cli integration + begin + try_class = [self.to_s, 'Cli'].join('::') + @cli = Kernel.const_get(try_class) + rescue NameError + nil + end + else + raise ArgumentError, "Expecting a class. Given: #{cli_class.class} object" + end + end end - inheritable_class_vars :type + inheritable_class_vars :type, :cli def initialize(usecases) raise "Expected Eco::API::UseCases. Given #{usecases.class}" unless usecases.is_a?(Eco::API::UseCases) usecases.define(self.name, type: self.type, &self.method(:main)) end @@ -30,9 +52,13 @@ raise "You should implement this method" end def type self.class.type + end + + def cli_apply! + self.class.cli! end end end end end