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
# 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



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

def add_option(arg, desc = nil, &block)
  tap do
    "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



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

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



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

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



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

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

#nameObject



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

def name
  usecase.name
end

#optionsObject



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

def options
  @options ||= {}
end

#typeObject



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

def type
  usecase.type
end