Class: Vop::Command
- Inherits:
-
Object
- Object
- Vop::Command
- Defined in:
- lib/vop/objects/command.rb
Instance Attribute Summary collapse
-
#allows_extra ⇒ Object
Returns the value of attribute allows_extra.
-
#block ⇒ Object
Returns the value of attribute block.
-
#description ⇒ Object
Returns the value of attribute description.
-
#dont_register ⇒ Object
Returns the value of attribute dont_register.
-
#invalidation_block ⇒ Object
Returns the value of attribute invalidation_block.
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#plugin ⇒ Object
Returns the value of attribute plugin.
-
#read_only ⇒ Object
Returns the value of attribute read_only.
-
#show_options ⇒ Object
Returns the value of attribute show_options.
Instance Method Summary collapse
- #add_param(name, options = {}) ⇒ Object
-
#default_param ⇒ 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.
- #execute(payload) ⇒ Object
-
#initialize(plugin, name) ⇒ Command
constructor
A new instance of Command.
- #mandatory_params ⇒ Object
- #param(name) ⇒ Object
- #params_with(&filter) ⇒ Object
- #short_name ⇒ Object
- #source ⇒ Object
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_extra ⇒ Object
Returns the value of attribute allows_extra.
18 19 20 |
# File 'lib/vop/objects/command.rb', line 18 def allows_extra @allows_extra end |
#block ⇒ Object
Returns the value of attribute block.
10 11 12 |
# File 'lib/vop/objects/command.rb', line 10 def block @block end |
#description ⇒ Object
Returns the value of attribute description.
12 13 14 |
# File 'lib/vop/objects/command.rb', line 12 def description @description end |
#dont_register ⇒ Object
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_block ⇒ Object
Returns the value of attribute invalidation_block.
13 14 15 |
# File 'lib/vop/objects/command.rb', line 13 def invalidation_block @invalidation_block end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/vop/objects/command.rb', line 8 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
11 12 13 |
# File 'lib/vop/objects/command.rb', line 11 def params @params end |
#plugin ⇒ Object
Returns the value of attribute plugin.
7 8 9 |
# File 'lib/vop/objects/command.rb', line 7 def plugin @plugin end |
#read_only ⇒ Object
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_options ⇒ Object
Returns the value of attribute show_options.
15 16 17 |
# File 'lib/vop/objects/command.rb', line 15 def @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, = {}) @params << CommandParam.new(name, ) end |
#default_param ⇒ 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
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.[: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_params ⇒ Object
58 59 60 |
# File 'lib/vop/objects/command.rb', line 58 def mandatory_params params_with { |x| x.[: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_name ⇒ Object
36 37 38 |
# File 'lib/vop/objects/command.rb', line 36 def short_name @name.split(".").last end |
#source ⇒ Object
40 41 42 |
# File 'lib/vop/objects/command.rb', line 40 def source plugin.sources[:commands][name] end |