lib/eco/api/usecases/cli/dsl.rb in eco-helpers-2.6.0 vs lib/eco/api/usecases/cli/dsl.rb in eco-helpers-2.6.1

- old
+ new

@@ -1,26 +1,35 @@ module Eco module API class UseCases class Cli module DSL - # Links the usecase to the Cli via `option_case` - def apply!(option_case = cli_name) - cli_config_case(option_case) - apply_options(option_case) - applied! + # Links the usecase to the Cli via `arg_case` + def apply!(arg_case = cli_name) + #puts "DEFINING CLI for '#{arg_case}' via #{self}" + if applied?(arg_case) + puts "Warning: (#{self}) Tried to call again cli.apply! on '#{arg_case}'" + return self + end + cli_config_case(arg_case) + apply_options(arg_case) + applied!(arg_case) self end - def applied? - @applied || false + def applied + @applied ||= {} end - def applied! - @applied = true + def applied?(arg_case) + applied[arg_case] || false end + def applied!(arg_case) + applied[arg_case] = true + end + attr_writer :usecase # Unless specified, assume Cli class hangs from its case namespace def usecase raise "#{self} is to use to extend a class" unless self.is_a?(Class) @usecase ||= Kernel.const_get(self.to_s.split('::')[0..-2].join('::')) @@ -46,10 +55,11 @@ value end end def callback(&block) + return @callback unless block_given? @callback = block end def options @options ||= {} @@ -62,17 +72,17 @@ end end private - def apply_options(option_case) + def apply_options(arg_case) options.each do |_key, option| - option.link_case(cli_config_case(option_case)) + option.link_case(cli_config_case(arg_case)) end end - def cli_config_case(option_case = nil) - @cli_config_case ||= usecases.add(option_case, type, desc, case_name: name, &callback) + def cli_config_case(arg_case = nil) + @cli_config_case ||= usecases.add(arg_case, type, desc, case_name: name, &callback) end def usecases(&block) ::ASSETS.cli.config.usecases(&block) end