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



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

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



72
73
74
75
76
77
# File 'lib/eco/api/usecases/cli/dsl.rb', line 72

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



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

def applied
  @applied ||= {}
end

#applied!(arg_case) ⇒ Object



28
29
30
# File 'lib/eco/api/usecases/cli/dsl.rb', line 28

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

#applied?(arg_case) ⇒ Boolean

Returns:

  • (Boolean)


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

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
16
17
18
# File 'lib/eco/api/usecases/cli/dsl.rb', line 7

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

#callback(&block) ⇒ Object



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

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



55
56
57
58
59
60
# File 'lib/eco/api/usecases/cli/dsl.rb', line 55

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



41
42
43
# File 'lib/eco/api/usecases/cli/dsl.rb', line 41

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

#nameObject



50
51
52
# File 'lib/eco/api/usecases/cli/dsl.rb', line 50

def name
  usecase.name
end

#optionsObject



68
69
70
# File 'lib/eco/api/usecases/cli/dsl.rb', line 68

def options
  @options ||= {}
end

#typeObject



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

def type
  usecase.type
end