Class: Boson::Command
- Inherits:
-
Object
- Object
- Boson::Command
- Extended by:
- APIClassMethods
- Includes:
- API
- Defined in:
- lib/boson/command.rb
Overview
A command starts with the functionality of a ruby method and adds benefits with options, etc.
Defined Under Namespace
Modules: API, APIClassMethods
Constant Summary collapse
- ATTRIBUTES =
Attributes that are defined as accessors
[:name, :lib, :alias, :desc, :options, :args, :config]
- INIT_ATTRIBUTES =
Attributes that can be passed in at initialization
[:alias, :desc, :options, :default_option, :option_command]
Instance Attribute Summary collapse
-
#file_parsed_args ⇒ Object
readonly
Returns the value of attribute file_parsed_args.
Class Method Summary collapse
-
.usage(command) ⇒ Object
One line usage for a command if it exists.
Instance Method Summary collapse
-
#arg_size ⇒ Object
Number of arguments.
- #args(lib = library) ⇒ Object
-
#config ⇒ Object
until @config is consistent in index + actual loading.
-
#has_splat_args? ⇒ Boolean
Indicates if any arg has a splat.
-
#incorrect_arg_size?(args) ⇒ Boolean
Determines if incorrect # of args given i.e.
-
#initialize(attributes) ⇒ Command
constructor
Takes a hash of attributes which map to instance variables and values.
-
#library ⇒ Object
Library object a command belongs to.
-
#numerical_arg_size? ⇒ Boolean
Indicates if arg size can handle a numerical comparison.
-
#option_command? ⇒ Boolean
Indicates if an OptionCommand.
-
#option_parser ⇒ Object
Option parser for command as defined by @options.
Methods included from APIClassMethods
create, find, library_attributes, new_attributes
Methods included from API
#after_initialize, #basic_usage, #full_name
Constructor Details
#initialize(attributes) ⇒ Command
Takes a hash of attributes which map to instance variables and values. :name and :lib are required keys.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/boson/command.rb', line 65 def initialize(attributes) hash = attributes.dup @name = hash.delete(:name) or raise ArgumentError @lib = hash.delete(:lib) or raise ArgumentError # since MethodInspector scrapes arguments from file by default @file_parsed_args = true INIT_ATTRIBUTES.each do |e| instance_variable_set("@#{e}", hash.delete(e)) if hash.key?(e) end after_initialize(hash) if (args = hash.delete(:args)) if args.is_a?(Array) @args = args elsif args.to_s[/^\d+/] @arg_size = args.to_i elsif args == '*' @args = [['*args']] end end @config = Util.recursive_hash_merge hash, hash.delete(:config) || {} end |
Instance Attribute Details
#file_parsed_args ⇒ Object (readonly)
Returns the value of attribute file_parsed_args.
40 41 42 |
# File 'lib/boson/command.rb', line 40 def file_parsed_args @file_parsed_args end |
Class Method Details
.usage(command) ⇒ Object
One line usage for a command if it exists
31 32 33 |
# File 'lib/boson/command.rb', line 31 def self.usage(command) (cmd = find(command)) ? "#{command} #{cmd.usage}" : '' end |
Instance Method Details
#arg_size ⇒ Object
Number of arguments
152 153 154 155 156 157 |
# File 'lib/boson/command.rb', line 152 def arg_size unless instance_variable_defined?("@arg_size") @arg_size = args ? args.size : nil end @arg_size end |
#args(lib = library) ⇒ Object
115 116 117 |
# File 'lib/boson/command.rb', line 115 def args(lib=library) @args end |
#config ⇒ Object
until @config is consistent in index + actual loading
130 131 132 |
# File 'lib/boson/command.rb', line 130 def config @config ||= {} end |
#has_splat_args? ⇒ Boolean
Indicates if any arg has a splat
135 136 137 |
# File 'lib/boson/command.rb', line 135 def has_splat_args? !!(args && @args[-1] && @args[-1][0][/^\*/]) end |
#incorrect_arg_size?(args) ⇒ Boolean
Determines if incorrect # of args given i.e. too little or too much
145 146 147 148 149 |
# File 'lib/boson/command.rb', line 145 def incorrect_arg_size?(args) return false if has_splat_args? required_arg_size = args.take_while {|e| e[1].nil? }.size args.size < required_arg_size || args.size > arg_size end |
#library ⇒ Object
Library object a command belongs to.
111 112 113 |
# File 'lib/boson/command.rb', line 111 def library @library ||= Boson.library(@lib) end |
#numerical_arg_size? ⇒ Boolean
Indicates if arg size can handle a numerical comparison
140 141 142 |
# File 'lib/boson/command.rb', line 140 def numerical_arg_size? !has_splat_args? && arg_size end |
#option_command? ⇒ Boolean
Indicates if an OptionCommand
125 126 127 |
# File 'lib/boson/command.rb', line 125 def option_command? || @option_command end |
#option_parser ⇒ Object
Option parser for command as defined by @options.
120 121 122 |
# File 'lib/boson/command.rb', line 120 def option_parser @option_parser ||= OptionParser.new(@options || {}) end |