Module: Rubikon::Application::DSLMethods
- Included in:
- Base
- Defined in:
- lib/rubikon/application/dsl_methods.rb
Overview
This module contains all DSL-related instance methods of
Application::Base and its subclasses. The methods of this module may be
used to define and enhance a Rubikon application.
Instance Attribute Summary collapse
-
#base_file ⇒ String
readonly
The (first) file where the application has been defined.
-
#config ⇒ Hash
readonly
The active configuration of the application.
-
#path ⇒ String
readonly
The absolute path of the application.
Instance Method Summary collapse
-
#active?(name) ⇒ Boolean
(also: #given?)
private
Checks whether parameter with the given name has been supplied by the user on the command-line.
-
#call(command_name, *args) ⇒ Object
private
Call another named command with the given arguments.
-
#command(name, *options, &block) ⇒ Command
private
Define a new application Command or an alias to an existing one.
-
#debug(message) ⇒ Object
private
Prints a debug message if $DEBUG is
true, e.g. -
#default(*options, &block) ⇒ Command
private
Define the default Command of the application, i.e.
-
#default_config=(config) ⇒ Object
private
Set the default configuration for this application.
-
#error(text = nil) ⇒ Object
private
Output a line of text using
IO#putsof the error output stream. -
#estream ⇒ IO
private
Convenience method for accessing the user-defined error output stream.
-
#flag(name, description = nil, &block) ⇒ Object
private
Create a new Flag with the given name for the next Command.
-
#global_flag(name, description = nil, &block) ⇒ Object
private
Create a new flag with the given name to be used globally.
-
#global_option(name, *options, &block) ⇒ Object
private
Create a new option with the given name to be used globally.
-
#input(prompt = '', *expected) ⇒ Object
private
Prompts the user for input.
-
#option(name, *options, &block) ⇒ Object
private
Create a new Option with the given name for the next Command.
-
#ostream ⇒ IO
private
Convenience method for accessing the user-defined output stream.
-
#post_execute(&block) ⇒ Object
private
Defines a block of code used as a hook that should be executed after the command execution has finished.
-
#post_init(&block) ⇒ Object
private
Defines a block of code used as a hook that should be executed after the application has been initialized.
-
#pre_execute(&block) ⇒ Object
private
Defines a block of code used as a hook that should be executed before the command has been started.
-
#pre_init(&block) ⇒ Object
private
Defines a block of code used as a hook that should be executed before the application has been initialized.
-
#progress_bar(*options, &block) {|ProgressBar| ... } ⇒ Object
private
Displays a progress bar while the given block is executed.
-
#put(text) ⇒ Object
private
Output text using
IO#<<of the output stream. -
#putc(char) ⇒ Object
private
Output a character using
IO#putcof the output stream. -
#puts(text = nil) ⇒ Object
private
Output a line of text using
IO#putsof the output stream. -
#save_config(scope = :user) ⇒ Object
private
Saves the current configuration into a configuration file.
-
#set(setting, value) ⇒ Object
private
Sets an application setting.
-
#throbber(&block) { ... } ⇒ Object
private
Displays a throbber while the given block is executed.
Instance Attribute Details
#base_file ⇒ String (readonly)
Returns The (first) file where the application has been defined.
23 24 25 |
# File 'lib/rubikon/application/dsl_methods.rb', line 23 def base_file @base_file end |
#config ⇒ Hash (readonly)
Returns The active configuration of the application.
26 27 28 |
# File 'lib/rubikon/application/dsl_methods.rb', line 26 def config @config end |
#path ⇒ String (readonly)
Returns The absolute path of the application.
29 30 31 |
# File 'lib/rubikon/application/dsl_methods.rb', line 29 def path @path end |
Instance Method Details
#active?(name) ⇒ Boolean (private) Also known as: given?
Checks whether parameter with the given name has been supplied by the user on the command-line.
45 46 47 48 49 50 51 |
# File 'lib/rubikon/application/dsl_methods.rb', line 45 def active?(name) name = name.to_sym parameter = @global_parameters[name] parameter = @current_command.parameters[name] if parameter.nil? return false if parameter.nil? parameter.send(:active?) end |
#call(command_name, *args) ⇒ Object (private)
Call another named command with the given arguments
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rubikon/application/dsl_methods.rb', line 60 def call(command_name, *args) args.extend ArgumentVector current_command = @current_command current_param = @current_param @current_command = @commands[command_name] args.params!(@current_command.params).each do |param| @current_param = param param.send :active! @current_param = nil end @current_command.send :run @current_command = current_command @current_param = current_param end |
#command(name, *options, &block) ⇒ Command (private)
Define a new application Command or an alias to an existing one
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 121 122 123 |
# File 'lib/rubikon/application/dsl_methods.rb', line 91 def command(name, *, &block) command = nil if name.is_a? Hash name.each do |alias_name, command_name| command = @commands[command_name] if command.nil? @commands[alias_name] = command_name else command.aliases << alias_name @commands[alias_name] = command end end else command = Command.new(self, name, *, &block) @commands.each do |command_alias, command_name| if command_name == command.name @commands[command_alias] = command command.aliases << command_alias end end @commands[command.name] = command end unless command.nil? || @parameters.empty? @parameters.each do |parameter| command.send(:add_param, parameter) end @parameters.clear end command end |
#debug(message) ⇒ Object (private)
Prints a debug message if $DEBUG is true, e.g. if the user
supplied the --debug (-d) flag.
129 130 131 |
# File 'lib/rubikon/application/dsl_methods.rb', line 129 def debug() ostream.puts if $DEBUG end |
#default(*options, &block) ⇒ Command (private)
Define the default Command of the application, i.e. the Command that is called if no matching Command parameter can be found
153 154 155 156 157 158 159 |
# File 'lib/rubikon/application/dsl_methods.rb', line 153 def default(*, &block) if .size == 1 && .first.is_a?(Symbol) && !block_given? command :__default => .first else command :__default, *, &block end end |
#default_config=(config) ⇒ Object (private)
Set the default configuration for this application
166 167 168 169 170 171 172 |
# File 'lib/rubikon/application/dsl_methods.rb', line 166 def default_config=(config) unless config.is_a? Hash raise ArgumentError.new('Configuration has to be a Hash') end @default_config = config end |
#error(text = nil) ⇒ Object (private)
Output a line of text using IO#puts of the error output stream
178 179 180 |
# File 'lib/rubikon/application/dsl_methods.rb', line 178 def error(text = nil) estream.puts text end |
#estream ⇒ IO (private)
Convenience method for accessing the user-defined error output stream
Use this if you want to work directly with the error output stream
191 192 193 |
# File 'lib/rubikon/application/dsl_methods.rb', line 191 def estream @settings[:estream] end |
#flag(name, description = nil, &block) ⇒ Object (private)
Create a new Flag with the given name for the next Command
212 213 214 215 216 217 218 219 220 |
# File 'lib/rubikon/application/dsl_methods.rb', line 212 def flag(name, description = nil, &block) if name.is_a? Hash @parameters << name else flag = Flag.new(self, name, &block) flag.description = description unless description.nil? @parameters << flag end end |
#global_flag(name, description = nil, &block) ⇒ Object (private)
Create a new flag with the given name to be used globally
Global flags are not bound to any command and can therefore be used throughout the application with the same result.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/rubikon/application/dsl_methods.rb', line 240 def global_flag(name, description = nil, &block) if name.is_a? Hash name.each do |alias_name, flag_name| flag = @global_parameters[flag_name] if flag.nil? @global_parameters[alias_name] = flag_name else flag.aliases << alias_name @global_parameters[alias_name] = flag end end else flag = Flag.new(self, name, &block) flag.description = description unless description.nil? @global_parameters.each do |flag_alias, flag_name| if flag_name == flag.name @global_parameters[flag_alias] = flag flag.aliases << flag_alias end end @global_parameters[flag.name] = flag end end |
#global_option(name, *options, &block) ⇒ Object (private)
Create a new option with the given name to be used globally
Global options are not bound to any command and can therefore be used throughout the application with the same result.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rubikon/application/dsl_methods.rb', line 282 def global_option(name, *, &block) if name.is_a? Hash name.each do |alias_name, option_name| option = @global_parameters[option_name] if option.nil? @global_parameters[alias_name] = option_name else option.aliases << alias_name @global_parameters[alias_name] = option end end else option = Option.new(self, name, *, &block) @global_parameters.each do |option_alias, option_name| if option_name == option.name @global_parameters[option_alias] = option option.aliases << option_alias end end @global_parameters[option.name] = option end end |
#input(prompt = '', *expected) ⇒ Object (private)
Prompts the user for input
327 328 329 330 331 332 333 334 335 |
# File 'lib/rubikon/application/dsl_methods.rb', line 327 def input(prompt = '', *expected) prompt << " [#{expected.join '/'}]" unless expected.empty? ostream << "#{prompt}: " unless prompt.to_s.empty? input = @settings[:istream].gets[0..-2] unless expected.empty? || expected.include?(input) input = input 'Please provide valid input', *expected end input end |
#option(name, *options, &block) ⇒ Object (private)
Create a new Option with the given name for the next Command
356 357 358 359 360 361 362 363 |
# File 'lib/rubikon/application/dsl_methods.rb', line 356 def option(name, *, &block) if name.is_a? Hash @parameters << name else option = Option.new(self, name.to_s, *, &block) @parameters << option end end |
#ostream ⇒ IO (private)
Convenience method for accessing the user-defined output stream
Use this if you want to work directly with the output stream
374 375 376 |
# File 'lib/rubikon/application/dsl_methods.rb', line 374 def ostream @settings[:ostream] end |
#post_execute(&block) ⇒ Object (private)
Defines a block of code used as a hook that should be executed after the command execution has finished
384 385 386 |
# File 'lib/rubikon/application/dsl_methods.rb', line 384 def post_execute(&block) @hooks[:post_execute] = block end |
#post_init(&block) ⇒ Object (private)
Defines a block of code used as a hook that should be executed after the application has been initialized
394 395 396 |
# File 'lib/rubikon/application/dsl_methods.rb', line 394 def post_init(&block) @hooks[:post_init] = block end |
#pre_execute(&block) ⇒ Object (private)
Defines a block of code used as a hook that should be executed before the command has been started
404 405 406 |
# File 'lib/rubikon/application/dsl_methods.rb', line 404 def pre_execute(&block) @hooks[:pre_execute] = block end |
#pre_init(&block) ⇒ Object (private)
Defines a block of code used as a hook that should be executed before the application has been initialized
414 415 416 |
# File 'lib/rubikon/application/dsl_methods.rb', line 414 def pre_init(&block) @hooks[:pre_init] = block end |
#progress_bar(*options, &block) {|ProgressBar| ... } ⇒ Object (private)
Displays a progress bar while the given block is executed
Inside the block you have access to a instance of ProgressBar. So you can update the progress using ProgressBar#+.
441 442 443 444 445 446 447 448 449 450 |
# File 'lib/rubikon/application/dsl_methods.rb', line 441 def (*, &block) hidden_output do |ostream| = [0] [:ostream] = ostream progress = ProgressBar.new() block.call(progress) end end |
#put(text) ⇒ Object (private)
Output text using IO#<< of the output stream
456 457 458 459 |
# File 'lib/rubikon/application/dsl_methods.rb', line 456 def put(text) ostream << text ostream.flush end |
#putc(char) ⇒ Object (private)
Output a character using IO#putc of the output stream
466 467 468 |
# File 'lib/rubikon/application/dsl_methods.rb', line 466 def putc(char) ostream.putc char end |
#puts(text = nil) ⇒ Object (private)
Output a line of text using IO#puts of the output stream
474 475 476 |
# File 'lib/rubikon/application/dsl_methods.rb', line 474 def puts(text = nil) ostream.puts text end |
#save_config(scope = :user) ⇒ Object (private)
Saves the current configuration into a configuration file
The file name and format are specified in the application settings.
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/rubikon/application/dsl_methods.rb', line 523 def save_config(scope = :user) case scope when :global if RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|bccwin/ path = ENV['ALLUSERSPROFILE'] else path = '/etc' end when :local path = File. '.' when :user path = File. '~' else path = scope end @config_factory.save_config @config, File.join(path, @settings[:config_file]) end |
#set(setting, value) ⇒ Object (private)
Sets an application setting
Available settings
autorunIf
true, let the application run as soon as its class is defined. This is generally useful for simple "code and run" applications.colorsIf
true, enables colored output using ColoredIOconfig_fileThe name of the config file to search
config_pathsThe paths to search for config files
estreamDefines an error output stream to use
help_bannerDefines a banner for the help message
istreamDefines an input stream to use
nameDefines the name of the application
ostreamDefines an output stream to use
raise_errorsIf
true, raise errors, otherwise fail gracefully
501 502 503 504 505 506 507 508 509 510 |
# File 'lib/rubikon/application/dsl_methods.rb', line 501 def set(setting, value) setting = setting.to_sym if setting == :estream self.estream = value elsif setting == :ostream self.ostream = value else @settings[setting.to_sym] = value end end |
#throbber(&block) { ... } ⇒ Object (private)
Displays a throbber while the given block is executed
558 559 560 561 562 563 564 565 566 |
# File 'lib/rubikon/application/dsl_methods.rb', line 558 def throbber(&block) hidden_output do |ostream| code_thread = Thread.new { block.call } throbber_thread = Throbber.new(ostream, code_thread) code_thread.join throbber_thread.join end end |