Class: Vop::CommandParam

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/objects/command_param.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ CommandParam

Returns a new instance of CommandParam.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vop/objects/command_param.rb', line 7

def initialize(name, options = {})
  @name = name

  unless options.is_a? Hash
    raise "[CommandParam] sanity check failed: unexpected options object class #{options.class}, expected Hash"
  end

  # auto-detect boolean parameters
  if options.has_key? :default
    if options[:default] == true || options[:default] == false
      options[:boolean] = true
    end
  end

  defaults = {
    multi: false,
    mandatory: false,
    default_param: false
  }
  @options = defaults.merge(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/vop/objects/command_param.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/vop/objects/command_param.rb', line 5

def options
  @options
end

Instance Method Details

#lookup(current_values = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vop/objects/command_param.rb', line 29

def lookup(current_values = {})
  begin
    return [] unless lookup_block = options[:lookup]

    # the lookup block might want the previously collected params as input
    lookups = if lookup_block.arity > 0
      lookup_block.call(current_values)
    else
      lookup_block.call()
    end
  rescue => detail
    $logger.error "problem loading lookup values for #{name} : #{detail.message}"
  end
end

#to_json(options) ⇒ Object



52
53
54
55
56
57
# File 'lib/vop/objects/command_param.rb', line 52

def to_json(options)
  {
    name: @name,
    options: @options
  }.to_json(options)
end

#wants_contextObject

some params do not want to prefilled from the context



45
46
47
48
49
50
# File 'lib/vop/objects/command_param.rb', line 45

def wants_context
  !(
    options.has_key?(:use_context) &&
    options[:use_context] == false
   )
end