Class: Rubikon::Command
- Includes:
- HasArguments
- Defined in:
- lib/rubikon/command.rb
Overview
Instances of the Command class are used to define the real code that should be executed when running the Application.
Constant Summary
Constants included from HasArguments
HasArguments::ARGUMENT_MATCHERS
Instance Attribute Summary collapse
-
#description ⇒ String
The description of this command.
-
#params ⇒ Array<Parameter>
(also: #parameters)
readonly
The parameters of this command.
Attributes included from Parameter
Instance Method Summary collapse
-
#active_params ⇒ Array<Parameter>
private
Returns all parameters of this command that are active, i.e.
-
#add_param(parameter) ⇒ Object
private
Add a new parameter for this command.
-
#help(show_usage = true) ⇒ String
Generate help for this command.
-
#initialize(app, name, *options, &block) ⇒ Command
constructor
Create a new application command with the given name with a reference to the app it belongs to.
-
#method_missing(name, *args, &block) ⇒ Object
private
If a parameter with the specified method name exists, a call to that method will return the value of the parameter.
-
#reset ⇒ Object
private
Resets this command to its initial state.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
private
Checks whether a parameter with the given name exists for this command.
-
#run ⇒ Object
private
Run this command’s code block.
Methods included from HasArguments
Methods included from Parameter
Constructor Details
#initialize(app, name, *options, &block) ⇒ Command
Create a new application command with the given name with a reference to the app it belongs to
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rubikon/command.rb', line 44 def initialize(app, name, *, &block) super @params = {} if block_given? @block = block else @file_name = "#{@app.path}/commands/#{name}.rb" raise BlockMissingError unless File.exists?(@file_name) code = open(@file_name).read @block = Proc.new { instance_eval(code) } end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
If a parameter with the specified method name exists, a call to that method will return the value of the parameter.
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/rubikon/command.rb', line 177 def method_missing(name, *args, &block) if args.empty? && !block_given? if @params.key?(name) return @params[name] else active_params.each do |param| return param.send(name) if param.respond_to_missing?(name) end end end super end |
Instance Attribute Details
#description ⇒ String
Returns The description of this command.
25 26 27 |
# File 'lib/rubikon/command.rb', line 25 def description @description end |
#params ⇒ Array<Parameter> (readonly) Also known as: parameters
Returns The parameters of this command.
28 29 30 |
# File 'lib/rubikon/command.rb', line 28 def params @params end |
Instance Method Details
#active_params ⇒ Array<Parameter> (private)
Returns all parameters of this command that are active, i.e. that have been supplied on the command-line
130 131 132 |
# File 'lib/rubikon/command.rb', line 130 def active_params @params.values.select { |param| param.active? } end |
#add_param(parameter) ⇒ Object (private)
Add a new parameter for this command
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/rubikon/command.rb', line 141 def add_param(parameter) if parameter.is_a? Hash parameter.each do |alias_name, name| alias_name = alias_name.to_sym name = name.to_sym parameter = @params[name] if parameter.nil? @params[alias_name] = name else parameter.aliases << alias_name @params[alias_name] = parameter end end else raise ArgumentError unless parameter.is_a? Parameter @params.each do |name, param| if param == parameter.name parameter.aliases << name @params[name] = parameter end end @params[parameter.name] = parameter end end |
#help(show_usage = true) ⇒ String
Generate help for this command
65 66 67 68 69 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 116 117 118 119 120 |
# File 'lib/rubikon/command.rb', line 65 def help(show_usage = true) help = '' if show_usage help << " #{name}" if name != :__default @params.values.uniq.sort_by {|a| a.name.to_s }.each do |param| help << ' [' ([param.name] + param.aliases).each_with_index do |name, index| name = name.to_s help << '|' if index > 0 help << '-' if name.size > 1 help << "-#{name}" end help << ' ...' if param.is_a?(Option) help << ']' end end help << "\n\n#{description}" unless description.nil? help_flags = {} = {} params.each_value do |param| if param.is_a? Flag help_flags[param.name.to_s] = param else [param.name.to_s] = param end end param_name = lambda { |name| "#{name.size > 1 ? '-' : ' '}-#{name}" } unless help_flags.empty? && .empty? max_param_length = (help_flags.keys + .keys). max_by { |a| a.size }.size + 2 end unless help_flags.empty? help << "\n\nFlags:" help_flags.sort_by { |name, param| name }.each do |name, param| help << "\n #{param_name.call(name).ljust(max_param_length)}" help << " #{param.description}" unless param.description.nil? end end unless .empty? help << "\n\nOptions:\n" .sort_by { |name, param| name }.each do |name, param| help << " #{param_name.call(name).ljust(max_param_length)} ..." help << " #{param.description}" unless param.description.nil? help << "\n" end end help end |
#reset ⇒ Object (private)
Resets this command to its initial state
195 196 197 198 199 200 |
# File 'lib/rubikon/command.rb', line 195 def reset super @params.values.uniq.each do |param| param.send(:reset) if param.is_a? Parameter end end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean (private)
Checks whether a parameter with the given name exists for this command
This is used to determine if a method call would successfully return the value of a parameter.
209 210 211 212 213 |
# File 'lib/rubikon/command.rb', line 209 def respond_to_missing?(name, include_private = false) @params.key?(name) || active_params.any? { |param| param.respond_to_missing?(name) } || super end |
#run ⇒ Object (private)
Run this command’s code block
216 217 218 219 220 |
# File 'lib/rubikon/command.rb', line 216 def run check_args Application::InstanceMethods.instance_method(:sandbox).bind(@app).call. instance_eval(&@block) end |