Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codebase refactoring #451

Closed
wants to merge 13 commits into from
21 changes: 13 additions & 8 deletions lib/hub/args.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
require 'delegate'

module Hub
# 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.
class Args < Array
class Args < SimpleDelegator
attr_accessor :executable

def initialize(*args)
super
@executable = ENV["GIT"] || "git"

@executable = ENV['GIT'] || 'git'
@skip = @noop = false
@original_args = args.first
@chain = [nil]
Expand All @@ -19,18 +22,19 @@ def initialize(*args)
# Adds an `after` callback.
# A callback can be a command or a proc.
def after(cmd_or_args = nil, args = nil, &block)
@chain.insert(-1, normalize_callback(cmd_or_args, args, block))
@chain << normalize_callback(cmd_or_args, args, block)
end

# Adds a `before` callback.
# A callback can be a command or a proc.
def before(cmd_or_args = nil, args = nil, &block)
@chain.insert(@chain.index(nil), normalize_callback(cmd_or_args, args, block))
@chain.insert(
@chain.index(nil), normalize_callback(cmd_or_args, args, block))
end

# Tells if there are multiple (chained) commands or not.
def chained?
@chain.size > 1
@chain.any?
end

# Returns an array of all commands.
Expand Down Expand Up @@ -89,11 +93,12 @@ def flags

# Tests if arguments were modified since instantiation
def changed?
chained? or self != @original_args
chained? || self != @original_args
end

def has_flag?(*flags)
pattern = flags.flatten.map { |f| Regexp.escape(f) }.join('|')
pattern = flags.flat_map { |f| Regexp.escape(f) }.join('|')

!grep(/^#{pattern}(?:=|$)/).empty?
end

Expand All @@ -109,7 +114,7 @@ def normalize_callback(cmd_or_args, args, block)
elsif cmd_or_args
cmd_or_args
else
raise ArgumentError, "command or block required"
raise ArgumentError, 'command or block required'
end
end
end
Expand Down
Loading