Class: Hub::Args

Inherits:
Array
  • Object
show all
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

Instance Method Summary collapse

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

#executableObject

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.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


79
80
81
# File 'lib/hub/args.rb', line 79

def changed?
  chained? or self != @original_args
end

#commandsObject

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

#flagsObject

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.

Returns:

  • (Boolean)


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

#wordsObject

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