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

Revert "New API for setting+configuring formatter: use_format" #295

Merged
merged 1 commit into from
Nov 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions lib/sshkit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Configuration
attr_writer :output, :backend, :default_env

def output
@output ||= use_format(:pretty)
@output ||= formatter(:pretty)
end

def deprecation_logger
Expand Down Expand Up @@ -34,29 +34,8 @@ def output_verbosity=(verbosity)
@output_verbosity = logger(verbosity)
end

# TODO: deprecate in favor of `use_format`
def format=(format)
use_format(format)
end

# Tell SSHKit to use the specified `formatter` for stdout. The formatter
# can be the name of a built-in SSHKit formatter, like `:pretty`, a
# formatter class, like `SSHKit::Formatter::Pretty`, or a custom formatter
# class you've written yourself.
#
# Additional arguments will be passed to the formatter's constructor.
#
# Example:
#
# config.use_format(:pretty)
#
# Is equivalent to:
#
# config.output = SSHKit::Formatter::Pretty.new($stdout)
#
def use_format(formatter, *args)
klass = formatter.is_a?(Class) ? formatter : formatter_class(formatter)
self.output = klass.new($stdout, *args)
self.output = formatter(format)
end

def command_map
Expand All @@ -73,13 +52,10 @@ def logger(verbosity)
verbosity.is_a?(Integer) ? verbosity : Logger.const_get(verbosity.upcase)
end

def formatter_class(symbol)
name = symbol.to_s.downcase
found = SSHKit::Formatter.constants.find do |const|
const.to_s.downcase == name
def formatter(format)
SSHKit::Formatter.constants.each do |const|
return SSHKit::Formatter.const_get(const).new($stdout) if const.downcase.eql?(format.downcase)
end
fail NameError, 'Unrecognized SSHKit::Formatter "#{symbol}"' if found.nil?
SSHKit::Formatter.const_get(found)
end

end
Expand Down
22 changes: 0 additions & 22 deletions test/unit/test_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,6 @@ def test_setting_formatter_types
end
end

def test_prohibits_unknown_formatter_type_with_exception
assert_raises(NameError) do
SSHKit.config.format = :doesnotexist
end
end

def test_options_can_be_provided_to_formatter
SSHKit.config.use_format(TestFormatter, :color => false)
formatter = SSHKit.config.output
assert_instance_of(TestFormatter, formatter)
assert_equal($stdout, formatter.output)
assert_equal({ :color => false }, formatter.options)
end

class TestFormatter
attr_accessor :output, :options

def initialize(output, options={})
@output = output
@options = options
end
end
end

end