Class: Vop::Command
- Inherits:
-
Object
- Object
- Vop::Command
- Defined in:
- lib/vop/command.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#plugin ⇒ Object
readonly
Returns the value of attribute plugin.
-
#show_options ⇒ Object
Returns the value of attribute show_options.
Instance Method Summary collapse
-
#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(param_values, extra = {}) ⇒ Object
-
#initialize(plugin, name) ⇒ Command
constructor
A new instance of Command.
- #inspect ⇒ Object
- #lookup(param_name, params) ⇒ Object
- #mandatory_params ⇒ Object
- #param(name) ⇒ Object
-
#prepare_params(ruby_args, extra = {}) ⇒ Object
accepts arguments as handed in by :define_method and prepares them into the
paramsstructure expected by command blocks. - #short_name ⇒ Object
- #source ⇒ Object
Constructor Details
#initialize(plugin, name) ⇒ Command
Returns a new instance of Command.
14 15 16 17 18 19 20 |
# File 'lib/vop/command.rb', line 14 def initialize(plugin, name) @plugin = plugin @name = name @block = lambda { |params| $logger.warn "#{name} not yet implemented!" } @params = [] = {} end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
8 9 10 |
# File 'lib/vop/command.rb', line 8 def block @block end |
#description ⇒ Object
Returns the value of attribute description.
9 10 11 |
# File 'lib/vop/command.rb', line 9 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/vop/command.rb', line 5 def name @name end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
12 13 14 |
# File 'lib/vop/command.rb', line 12 def params @params end |
#plugin ⇒ Object (readonly)
Returns the value of attribute plugin.
6 7 8 |
# File 'lib/vop/command.rb', line 6 def plugin @plugin end |
#show_options ⇒ Object
Returns the value of attribute show_options.
10 11 12 |
# File 'lib/vop/command.rb', line 10 def end |
Instance Method Details
#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’s only a single param, it’s the default param by default (ha!)
42 43 44 45 46 47 48 |
# File 'lib/vop/command.rb', line 42 def default_param if params.size == 1 params.first else params.select { |param| param[:default_param] == true }.first || nil end end |
#execute(param_values, extra = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/vop/command.rb', line 117 def execute(param_values, extra = {}) prepared = prepare_params(param_values, extra) block_param_names = self.block.parameters.map { |x| x.last } #puts "executing #{self.name} : prepared : #{prepared.inspect}" payload = [] context = {} # TODO should this be initialized? block_param_names.each do |name| param = nil case name.to_s when 'params' param = prepared when 'plugin' param = self.plugin when 'context' param = context when 'shell' raise "shell not supported" unless extra.has_key? 'shell' param = extra['shell'] else if prepared.has_key? name.to_s param = prepared[name.to_s] elsif prepared.has_key? name param = prepared[name] else raise "unknown block param name : >>#{name}<<" end end # from the black magick department: block parameters with the # same name as an entity get auto-inflated if param entity_names = @plugin.op.core.state[:entities].map { |entity| entity[:name] } if entity_names.include? name.to_s resolved = @plugin.op.send(name, param) param = resolved end payload << param end end result = self.block.call(*payload) [result, context] end |
#inspect ⇒ Object
22 23 24 |
# File 'lib/vop/command.rb', line 22 def inspect "Vop::Command #{@name}" end |
#lookup(param_name, params) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/vop/command.rb', line 56 def lookup(param_name, params) p = param(param_name) raise "no such param : #{param_name} in command #{name}" unless p if p.has_key? :lookup p[:lookup].call(params) else $logger.debug "no lookups for #{param_name}" [] end end |
#mandatory_params ⇒ Object
50 51 52 53 54 |
# File 'lib/vop/command.rb', line 50 def mandatory_params params.select do |p| p[:mandatory] end end |
#param(name) ⇒ Object
34 35 36 |
# File 'lib/vop/command.rb', line 34 def param(name) params.select { |param| param[:name] == name }.first || nil end |
#prepare_params(ruby_args, extra = {}) ⇒ Object
accepts arguments as handed in by :define_method and prepares them into the params structure expected by command blocks
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/vop/command.rb', line 70 def prepare_params(ruby_args, extra = {}) result = {} if ruby_args if ruby_args.is_a? Hash result = ruby_args ruby_args.each do |k,v| p = param(k) if p # values are auto-boxed into an array if the param expects multiple values if p[:multi] && ! v.is_a?(Array) then $logger.debug("autoboxing for #{p[:name]}") v = [ v ] # array values are auto-unboxed if the param doesn't want multi elsif ! p[:multi] && v.is_a?(Array) && v.length == 1 $logger.debug("autounboxing for #{p[:name]}") v = v.first end end result[k] = v end else # if there's a default param, it can be passed as "scalar" param # (as opposed to the usual hash) to execute, but will be converted # into a "normal" named param dp = default_param if dp result = {dp[:name] => ruby_args} end end end if extra.keys.size > 0 result.merge! extra end # add in defaults (for all params that have not been specified) params.each do |p| unless result.has_key? p[:name] if p[:default] result[p[:name]] = p[:default] end end end result end |
#short_name ⇒ Object
30 31 32 |
# File 'lib/vop/command.rb', line 30 def short_name name.split(".").last end |
#source ⇒ Object
26 27 28 |
# File 'lib/vop/command.rb', line 26 def source @plugin.command_source(name) end |