Class: Bovem::Command
- Inherits:
-
Object
- Object
- Bovem::Command
- Includes:
- Bovem::CommandMethods::Children, Bovem::CommandMethods::Help
- Defined in:
- lib/bovem/command.rb
Overview
This class represent a command (action) for Bovem.
Every command has the execution block and a set of option. Optionally, it also has before and after hooks.
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Proc|Symbol|NilClass) action(method = nil, &hook)
Reads and optionally sets the action of this command.
-
- (Proc|Symbol|NilClass) after(method = nil, &hook)
Sets the after hook, that is a block executed after the action of this command.
-
- (Application) application
Returns the application this command belongs to.
-
- (Array) arguments
readonly
The arguments provided to this command.
-
- (String) banner(value = nil)
Reads and optionally sets the description of this command.
-
- (Proc|Symbol|NilClass) before(method = nil, &hook)
Reads and optionally sets the before hook, that is a block executed before the action of this command.
-
- (Array) commands
readonly
The subcommands associated to this command.
-
- (String) description(value = nil)
Reads and optionally sets the short description of this command.
-
- (I18n) i18n
readonly
A i18n helper.
-
- (String) name(value = nil)
Reads and optionally sets the name of this command.
-
- (Array) options
readonly
The options available for this command.
-
- (Command) parent
The parent of this command.
-
- (String) synopsis(value = nil)
Reads and optionally sets the synopsis of this command.
Instance Method Summary (collapse)
-
- (Boolean) application?
Checks if the command is an application.
-
- (Boolean) banner?
Check if this command has a banner.
-
- (Boolean) description?
Check if this command has a description.
-
- (Object) execute(args)
Executes this command, running its action or a subcommand.
-
- (String) full_name(suffix = nil, separator = ":")
Gets a full name, that is the name of this command and its ancestor.
-
- (Command) initialize(options = {}, &block)
constructor
Creates a new command.
-
- (Command) setup_with(options = {})
Setups the command.
Methods included from Bovem::CommandMethods::Children
#argument, #clear_commands, #clear_options, #command, #commands?, #get_options, #option, #options?
Methods included from Bovem::CommandMethods::Help
Constructor Details
- (Command) initialize(options = {}, &block)
Creates a new command.
327 328 329 330 |
# File 'lib/bovem/command.rb', line 327 def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end |
Instance Attribute Details
- (Proc|Symbol|NilClass) action(method = nil, &hook)
Reads and optionally sets the action of this command.
A command action is only executed if no subcommand is executed.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Proc|Symbol|NilClass) after(method = nil, &hook)
Sets the after hook, that is a block executed after the action of this command.
This hook is only executed if no subcommand is executed.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Application) application
Returns the application this command belongs to.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Array) arguments (readonly)
Returns The arguments provided to this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (String) banner(value = nil)
Reads and optionally sets the description of this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Proc|Symbol|NilClass) before(method = nil, &hook)
Reads and optionally sets the before hook, that is a block executed before the action of this command.
This hook is only executed if no subcommand is executed.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Array) commands (readonly)
Returns The subcommands associated to this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (String) description(value = nil)
Reads and optionally sets the short description of this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (I18n) i18n (readonly)
Returns A i18n helper.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (String) name(value = nil)
Reads and optionally sets the name of this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Array) options (readonly)
Returns The options available for this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (Command) parent
Returns The parent of this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
- (String) synopsis(value = nil)
Reads and optionally sets the synopsis of this command.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/bovem/command.rb', line 309 class Command attr_accessor :name attr_accessor :description attr_accessor :banner attr_accessor :synopsis attr_accessor :before attr_accessor :action attr_accessor :after attr_accessor :application attr_accessor :parent attr_reader :i18n include Bovem::CommandMethods::Help include Bovem::CommandMethods::Children # Creates a new command. # # @param options [Hash] The settings to initialize the command with. def initialize( = {}, &block) setup_with() instance_eval(&block) if block_given? end # Reads and optionally sets the name of this command. # # @param value [NilClass|Object] The new name of this command. # @return [String] The name of this command. def name(value = nil) @name = value unless value.nil? @name end # Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix # # @param suffix [String] A suffix to append. # @param separator [String] The separator to use for components. # @return [String] The full name. def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end # Reads and optionally sets the short description of this command. # # @param value [NilClass|Object] The new short description of this command. # @return [String] The short description of this command. def description(value = nil) @description = value unless value.nil? @description end # Reads and optionally sets the description of this command. # # @param value [NilClass|Object] The new description of this command. # @return [String] The description of this command. def (value = nil) @banner = value unless value.nil? @banner end # Reads and optionally sets the synopsis of this command. # # @param value [NilClass|Object] The new synopsis of this command. # @return [String] The synopsis of this command. def synopsis(value = nil) @synopsis = value unless value.nil? @synopsis end # Reads and optionally sets the before hook, that is a block executed before the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The before hook of this command. def before(method = nil, &hook) @before = assign_hook(method, &hook) if method || hook @before end # Reads and optionally sets the action of this command. # # A command action is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The action of this command. def action(method = nil, &hook) @action = assign_hook(method, &hook) if method || hook @action end # Sets the after hook, that is a block executed after the action of this command. # # This hook is only executed if no subcommand is executed. # # @param method [String|Symbol|NilClass] The method of the application to hookup. # @param hook [Proc] The block to hookup if method is not provided. # @return [Proc|Symbol|NilClass] The after hook of this command. def after(method = nil, &hook) @after = assign_hook(method, &hook) if method || hook @after end # Returns the application this command belongs to. # # @return [Application] The application this command belongs to or `self`, if the command is an Application. def application application? ? self : @application end # Checks if the command is an application. # # @return [Boolean] `true` if command is an application, `false` otherwise. def application? is_a?(Bovem::Application) end # Check if this command has a description. # # @return [Boolean] `true` if this command has a description, `false` otherwise. def description? description.present? end # Check if this command has a banner. # # @return [Boolean] `true` if this command has a banner, `false` otherwise. def .present? end # Setups the command. # # @param options [Hash] The settings for this command. # @return [Command] The command. def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end # Executes this command, running its action or a subcommand. # # @param args [Array] The arguments to pass to the command. def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end private # :nodoc: def setup_i18n() @i18n = Bovem::I18n.new([:locale], root: "bovem.application", path: Bovem::Application::LOCALE_ROOT) end # :nodoc: def assign_hook(method, &hook) assigned = nil assigned = method if method.is_a?(::String) || method.is_a?(::Symbol) assigned = hook if !assigned && hook && hook.arity == 1 assigned end # :nodoc: def execute_hook(hook) return unless hook hook.is_a?(::String) || hook.is_a?(::Symbol) ? application.send(hook, self) : hook.call(self) end # :nodoc: def perform_action execute_hook(before) # Run the action execute_hook(action) # Run the after hook execute_hook(after) end end |
Instance Method Details
- (Boolean) application?
Checks if the command is an application.
424 425 426 |
# File 'lib/bovem/command.rb', line 424 def application? is_a?(Bovem::Application) end |
- (Boolean) banner?
Check if this command has a banner.
438 439 440 |
# File 'lib/bovem/command.rb', line 438 def .present? end |
- (Boolean) description?
Check if this command has a description.
431 432 433 |
# File 'lib/bovem/command.rb', line 431 def description? description.present? end |
- (Object) execute(args)
Executes this command, running its action or a subcommand.
466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/bovem/command.rb', line 466 def execute(args) subcommand = Bovem::Parser.parse(self, args) if subcommand.present? # We have a subcommand to call commands[subcommand[:name]].execute(subcommand[:args]) elsif action # Run our action # Run the before hook perform_action else # Show the help show_help end end |
- (String) full_name(suffix = nil, separator = ":")
Gets a full name, that is the name of this command and its ancestor. Optionally it also appends a suffix
346 347 348 349 |
# File 'lib/bovem/command.rb', line 346 def full_name(suffix = nil, separator = ":") return nil if application? [@parent ? @parent.full_name(nil, separator) : nil, !application? ? name : nil, suffix].compact.join(separator) end |
- (Command) setup_with(options = {})
Setups the command.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/bovem/command.rb', line 446 def setup_with( = {}) = {} unless .is_a?(::Hash) setup_i18n() .each_pair do |option, value| method = option.to_s if respond_to?(method) && self.method(method).arity != 0 send(method, value) elsif respond_to?(method + "=") send(method + "=", value) end end self end |