Module: Eco::API::UseCases::Cli::DSL

Included in:
Eco::API::UseCases::Cli
Defined in:
lib/eco/api/usecases/cli/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#usecaseObject

Unless specified, assume Cli class hangs from its case namespace



32
33
34
35
36
# File 'lib/eco/api/usecases/cli/dsl.rb', line 32

def usecase
  raise "#{self} is to use to extend a class" unless is_a?(Class)

  @usecase ||= Kernel.const_get(to_s.split('::')[0..-2].join('::'))
end

Instance Method Details

#add_option(arg, desc = nil, &block) ⇒ Object



69
70
71
72
73
74
# File 'lib/eco/api/usecases/cli/dsl.rb', line 69

def add_option(arg, desc = nil, &block)
  tap do
    puts "Overriding option '#{arg}' on case '#{name}'" if options.key?(arg)
    @options[arg] = Eco::API::UseCases::Cli::Option.new(arg, desc, &block)
  end
end

#appliedObject



17
18
19
# File 'lib/eco/api/usecases/cli/dsl.rb', line 17

def applied
  @applied ||= {}
end

#applied!(arg_case) ⇒ Object



25
26
27
# File 'lib/eco/api/usecases/cli/dsl.rb', line 25

def applied!(arg_case)
  applied[arg_case] = true
end

#applied?(arg_case) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/eco/api/usecases/cli/dsl.rb', line 21

def applied?(arg_case)
  applied[arg_case] || false
end

#apply!(arg_case = cli_name) ⇒ Object

Links the usecase to the Cli via arg_case



7
8
9
10
11
12
13
14
15
# File 'lib/eco/api/usecases/cli/dsl.rb', line 7

def apply!(arg_case = cli_name)
  # puts "Debug: (#{self}) Tried to call again cli.apply! on '#{arg_case}'"
  return self if applied?(arg_case)

  cli_config_case(arg_case)
  apply_options(arg_case)
  applied!(arg_case)
  self
end

#callback(&block) ⇒ Object



59
60
61
62
63
# File 'lib/eco/api/usecases/cli/dsl.rb', line 59

def callback(&block)
  return @callback unless block_given?

  @callback = block
end

#cli_name(arg_name = nil) ⇒ Object

It defaults to the use case preceded by dash



52
53
54
55
56
57
# File 'lib/eco/api/usecases/cli/dsl.rb', line 52

def cli_name(arg_name = nil)
  @cli_name = (arg_name.nil? ? @cli_name : arg_name).then do |value|
    value = "-#{name}" if value.nil?
    value
  end
end

#description(value = nil) ⇒ Object Also known as: desc



38
39
40
# File 'lib/eco/api/usecases/cli/dsl.rb', line 38

def description(value = nil)
  @description = (value.nil? ? @description : value)
end

#nameObject



47
48
49
# File 'lib/eco/api/usecases/cli/dsl.rb', line 47

def name
  usecase.name
end

#optionsObject



65
66
67
# File 'lib/eco/api/usecases/cli/dsl.rb', line 65

def options
  @options ||= {}
end

#typeObject



43
44
45
# File 'lib/eco/api/usecases/cli/dsl.rb', line 43

def type
  usecase.type
end