Class: Hub::Args
- Inherits:
-
Array
- Object
- Array
- Hub::Args
- Defined in:
- lib/hub/args.rb
Overview
The Args class exists to make it more convenient to work with command line arguments intended for git from within the Hub codebase.
The ARGV array is converted into an Args instance by the Hub instance when instantiated.
Instance Attribute Summary collapse
-
#executable ⇒ Object
Returns the value of attribute executable.
Instance Method Summary collapse
-
#after(cmd_or_args = nil, args = nil, &block) ⇒ Object
Adds an
aftercallback. -
#before(cmd_or_args = nil, args = nil, &block) ⇒ Object
Adds a
beforecallback. -
#chained? ⇒ Boolean
Tells if there are multiple (chained) commands or not.
-
#changed? ⇒ Boolean
Tests if arguments were modified since instantiation.
-
#commands ⇒ Object
Returns an array of all commands.
-
#flags ⇒ Object
All the flags (as opposed to words) contained in this argument list.
-
#initialize(*args) ⇒ Args
constructor
A new instance of Args.
-
#skip! ⇒ Object
Skip running this command.
-
#skip? ⇒ Boolean
Boolean indicating whether this command will run.
-
#to_exec(args = self) ⇒ Object
Array of
executablefollowed by all args suitable as arguments forexecorsystemcalls. -
#words ⇒ Object
All the words (as opposed to flags) contained in this argument list.
Constructor Details
#initialize(*args) ⇒ Args
Returns a new instance of Args.
11 12 13 14 15 16 17 18 |
# File 'lib/hub/args.rb', line 11 def initialize(*args) super @executable = ENV["GIT"] || "git" @after = nil @skip = false @original_args = args.first @chain = [nil] end |
Instance Attribute Details
#executable ⇒ Object
Returns the value of attribute executable.
9 10 11 |
# File 'lib/hub/args.rb', line 9 def executable @executable end |
Instance Method Details
#after(cmd_or_args = nil, args = nil, &block) ⇒ Object
Adds an after callback. A callback can be a command or a proc.
22 23 24 |
# File 'lib/hub/args.rb', line 22 def after(cmd_or_args = nil, args = nil, &block) @chain.insert(-1, normalize_callback(cmd_or_args, args, block)) end |
#before(cmd_or_args = nil, args = nil, &block) ⇒ Object
Adds a before callback. A callback can be a command or a proc.
28 29 30 |
# File 'lib/hub/args.rb', line 28 def before(cmd_or_args = nil, args = nil, &block) @chain.insert(@chain.index(nil), normalize_callback(cmd_or_args, args, block)) end |
#chained? ⇒ Boolean
Tells if there are multiple (chained) commands or not.
33 34 35 |
# File 'lib/hub/args.rb', line 33 def chained? @chain.size > 1 end |
#changed? ⇒ Boolean
Tests if arguments were modified since instantiation
79 80 81 |
# File 'lib/hub/args.rb', line 79 def changed? chained? or self != @original_args end |
#commands ⇒ Object
Returns an array of all commands.
38 39 40 41 42 |
# File 'lib/hub/args.rb', line 38 def commands chain = @chain.dup chain[chain.index(nil)] = self.to_exec chain end |
#flags ⇒ Object
All the flags (as opposed to words) contained in this argument list.
args = Args.new([ ‘remote’, ‘add’, ‘-f’, ‘tekkub’ ]) args.flags == [ ‘-f’ ]
74 75 76 |
# File 'lib/hub/args.rb', line 74 def flags self - words end |
#skip! ⇒ Object
Skip running this command.
45 46 47 |
# File 'lib/hub/args.rb', line 45 def skip! @skip ||= true end |
#skip? ⇒ Boolean
Boolean indicating whether this command will run.
50 51 52 |
# File 'lib/hub/args.rb', line 50 def skip? @skip end |
#to_exec(args = self) ⇒ Object
Array of executable followed by all args suitable as arguments for exec or system calls.
56 57 58 |
# File 'lib/hub/args.rb', line 56 def to_exec(args = self) [executable].concat args end |
#words ⇒ Object
All the words (as opposed to flags) contained in this argument list.
args = Args.new([ ‘remote’, ‘add’, ‘-f’, ‘tekkub’ ]) args.words == [ ‘remote’, ‘add’, ‘tekkub’ ]
65 66 67 |
# File 'lib/hub/args.rb', line 65 def words reject { |arg| arg.index('-') == 0 } end |