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



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

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('::'))
end

Instance Method Details

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



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

def add_option(arg, desc = nil, &block)
  self.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



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

def applied
  @applied ||= {}
end

#applied!(arg_case) ⇒ Object



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

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

#applied?(arg_case) ⇒ Boolean

Returns:

  • (Boolean)


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

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



59
60
61
62
# 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).yield_self 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



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

def options
  @options ||= {}
end

#typeObject



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

def type
  usecase.type
end