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
35
36
# 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
  @dont_log = 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_logObject

Returns the value of attribute dont_log.



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

def dont_log
  @dont_log
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



50
51
52
# File 'lib/vop/objects/command.rb', line 50

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

#default_param(values = {}) ⇒ Object

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vop/objects/command.rb', line 76

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

#execute(payload) ⇒ Object



91
92
93
# File 'lib/vop/objects/command.rb', line 91

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

#mandatory_params(values = {}) ⇒ Object



60
61
62
# File 'lib/vop/objects/command.rb', line 60

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

#missing_mandatory_params(values = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/vop/objects/command.rb', line 64

def missing_mandatory_params(values = {})
  mandatory_params.select do |param|
    ! values.keys.include?(param.name.to_sym) &&
    ! values.keys.include?(param.name.to_s)
  end
end

#param(name) ⇒ Object



46
47
48
# File 'lib/vop/objects/command.rb', line 46

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

#params_with(&filter) ⇒ Object



54
55
56
57
58
# File 'lib/vop/objects/command.rb', line 54

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

#short_nameObject



38
39
40
# File 'lib/vop/objects/command.rb', line 38

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

#sourceObject



42
43
44
# File 'lib/vop/objects/command.rb', line 42

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