Class: Vop::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, name) ⇒ Command

Returns a new instance of Command.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vop/objects/command.rb', line 20

def initialize(plugin, name)
  @plugin = plugin
  @name = name
  @description = nil

  @block = lambda { |params| $logger.warn "#{name} not yet implemented!" }
  @invalidation_block = nil

  @params = []
  @show_options = {}

  @dont_register = false
  @read_only = false
  @allows_extra = false
end

Instance Attribute Details

#allows_extraObject

Returns the value of attribute allows_extra.



18
19
20
# File 'lib/vop/objects/command.rb', line 18

def allows_extra
  @allows_extra
end

#blockObject

Returns the value of attribute block.



10
11
12
# File 'lib/vop/objects/command.rb', line 10

def block
  @block
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/vop/objects/command.rb', line 12

def description
  @description
end

#dont_registerObject

Returns the value of attribute dont_register.



16
17
18
# File 'lib/vop/objects/command.rb', line 16

def dont_register
  @dont_register
end

#invalidation_blockObject

Returns the value of attribute invalidation_block.



13
14
15
# File 'lib/vop/objects/command.rb', line 13

def invalidation_block
  @invalidation_block
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/vop/objects/command.rb', line 8

def name
  @name
end

#paramsObject

Returns the value of attribute params.



11
12
13
# File 'lib/vop/objects/command.rb', line 11

def params
  @params
end

#pluginObject

Returns the value of attribute plugin.



7
8
9
# File 'lib/vop/objects/command.rb', line 7

def plugin
  @plugin
end

#read_onlyObject

Returns the value of attribute read_only.



17
18
19
# File 'lib/vop/objects/command.rb', line 17

def read_only
  @read_only
end

#show_optionsObject

Returns the value of attribute show_options.



15
16
17
# File 'lib/vop/objects/command.rb', line 15

def show_options
  @show_options
end

Instance Method Details

#add_param(name, options = {}) ⇒ Object



48
49
50
# File 'lib/vop/objects/command.rb', line 48

def add_param(name, options = {})
  @params << CommandParam.new(name, options)
end

#default_paramObject

The default param is the one used when a command is called with a single “scalar” param only, like

@op.foo("zaphod")

If a parameter is marked as default, it will be assigned the value “zaphod” in this case. If there is only a single param, it is the default param by default Also, if there is only one mandatory param, it is considered to be the default param



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vop/objects/command.rb', line 67

def default_param
  if params.size == 1
    params.first
  else
    result = params_with { |x| x.options[:default_param] == true }.first
    if result.nil?
      mandatory = mandatory_params
      if mandatory_params.size == 1
        result = mandatory.first
      end
    end
    result
  end
end

#execute(payload) ⇒ Object



82
83
84
# File 'lib/vop/objects/command.rb', line 82

def execute(payload)
  self.block.call(*payload)
end

#mandatory_paramsObject



58
59
60
# File 'lib/vop/objects/command.rb', line 58

def mandatory_params
  params_with { |x| x.options[:mandatory] == true }
end

#param(name) ⇒ Object



44
45
46
# File 'lib/vop/objects/command.rb', line 44

def param(name)
  @params.select { |x| x.name == name }.first
end

#params_with(&filter) ⇒ Object



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

def params_with(&filter)
  params.select do |param|
    filter.call(param)
  end
end

#short_nameObject



36
37
38
# File 'lib/vop/objects/command.rb', line 36

def short_name
  @name.split(".").last
end

#sourceObject



40
41
42
# File 'lib/vop/objects/command.rb', line 40

def source
  plugin.sources[:commands][name]
end