aboutsummaryrefslogtreecommitdiffstats
path: root/Library
diff options
context:
space:
mode:
authorMarkus Reiter2017-05-20 03:19:41 +0200
committerMarkus Reiter2017-05-22 02:51:16 +0200
commitccafa1b75984f32e3e1fe5f860b561ada8876f96 (patch)
tree6e7a7508b1913dbad914fedab4fc68c20b131149 /Library
parented6823e659b54b8089a8b3aaf381025e0ed44761 (diff)
downloadbrew-ccafa1b75984f32e3e1fe5f860b561ada8876f96.tar.bz2
Refactor `CLI::InternalStanza`.
Diffstat (limited to 'Library')
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb56
1 files changed, 31 insertions, 25 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
index 303aa7ffe..a60a08d66 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb
@@ -51,70 +51,76 @@ module Hbc
]
def self.run(*args)
+ new(*args).run
+ end
+
+ def initialize(*args)
raise ArgumentError, "No stanza given." if args.empty?
- table = args.include? "--table"
- quiet = args.include? "--quiet"
- format = :to_yaml if args.include? "--yaml"
- format = :inspect if args.include? "--inspect"
- cask_tokens = cask_tokens_from(args)
- stanza = cask_tokens.shift.to_sym
- cask_tokens = Hbc.all_tokens if cask_tokens.empty?
+ @table = args.include? "--table"
+ @quiet = args.include? "--quiet"
+ @format = :to_yaml if args.include? "--yaml"
+ @format = :inspect if args.include? "--inspect"
+ @cask_tokens = self.class.cask_tokens_from(args)
+ @stanza = @cask_tokens.shift.to_sym
+ @cask_tokens = Hbc.all_tokens if @cask_tokens.empty?
+ end
- retval = print_stanzas(stanza, format, table, quiet, *cask_tokens)
+ def run
+ retval = print_stanzas
# retval is ternary: true/false/nil
if retval.nil?
- exit 1 if quiet
+ exit 1 if @quiet
raise CaskError, "nothing to print"
elsif !retval
- exit 1 if quiet
+ exit 1 if @quiet
raise CaskError, "print incomplete"
end
end
- def self.print_stanzas(stanza, format = nil, table = nil, quiet = nil, *cask_tokens)
+ def print_stanzas
count = 0
- if ARTIFACTS.include?(stanza)
- artifact_name = stanza
- stanza = :artifacts
+ if ARTIFACTS.include?(@stanza)
+ artifact_name = @stanza
+ @stanza = :artifacts
end
- cask_tokens.each do |cask_token|
- print "#{cask_token}\t" if table
+ @cask_tokens.each do |cask_token|
+ print "#{cask_token}\t" if @table
begin
cask = CaskLoader.load(cask_token)
rescue StandardError
- opoo "Cask '#{cask_token}' was not found" unless quiet
+ opoo "Cask '#{cask_token}' was not found" unless @quiet
puts ""
next
end
- unless cask.respond_to?(stanza)
- opoo "no such stanza '#{stanza}' on Cask '#{cask_token}'" unless quiet
+ unless cask.respond_to?(@stanza)
+ opoo "no such stanza '#{@stanza}' on Cask '#{cask_token}'" unless @quiet
puts ""
next
end
begin
- value = cask.send(stanza)
+ value = cask.send(@stanza)
rescue StandardError
- opoo "failure calling '#{stanza}' on Cask '#{cask_token}'" unless quiet
+ opoo "failure calling '#{@stanza}' on Cask '#{cask_token}'" unless @quiet
puts ""
next
end
if artifact_name && !value.key?(artifact_name)
- opoo "no such stanza '#{artifact_name}' on Cask '#{cask_token}'" unless quiet
+ opoo "no such stanza '#{artifact_name}' on Cask '#{cask_token}'" unless @quiet
puts ""
next
end
value = value.fetch(artifact_name).to_a.flatten if artifact_name
- if format
- puts value.send(format)
+ if @format
+ puts value.send(@format)
elsif artifact_name || value.is_a?(Symbol)
puts value.inspect
else
@@ -123,7 +129,7 @@ module Hbc
count += 1
end
- count.zero? ? nil : count == cask_tokens.length
+ count.zero? ? nil : count == @cask_tokens.length
end
def self.help