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_log ⇒ Object
Returns the value of attribute dont_log.
-
#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(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.
- #execute(payload) ⇒ Object
-
#initialize(plugin, name) ⇒ Command
constructor
A new instance of Command.
- #mandatory_params(values = {}) ⇒ Object
- #missing_mandatory_params(values = {}) ⇒ 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 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_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_log ⇒ Object
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_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
50 51 52 |
# File 'lib/vop/objects/command.rb', line 50 def add_param(name, = {}) @params << CommandParam.new(name, ) 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.[: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.[: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_name ⇒ Object
38 39 40 |
# File 'lib/vop/objects/command.rb', line 38 def short_name @name.split(".").last end |
#source ⇒ Object
42 43 44 |
# File 'lib/vop/objects/command.rb', line 42 def source plugin.sources[:commands][name] end |