class HammerCLI::Output::Output

Attributes

context[RW]
default_adapter[RW]

Public Class Methods

adapters() click to toggle source
# File lib/hammer_cli/output/output.rb, line 48
def self.adapters
  @adapters_hash ||= {}
  @adapters_hash
end
formatters() click to toggle source
# File lib/hammer_cli/output/output.rb, line 53
def self.formatters
  @formatters_hash ||= {}
  @formatters_hash
end
new(context={}, options={}) click to toggle source
# File lib/hammer_cli/output/output.rb, line 10
def initialize(context={}, options={})
  context[:verbosity] ||= HammerCLI::V_VERBOSE
  self.context = context
  self.default_adapter = options[:default_adapter]
end
register_adapter(name, adapter_class) click to toggle source
# File lib/hammer_cli/output/output.rb, line 58
def self.register_adapter(name, adapter_class)
  adapters[name] = adapter_class
end
register_formatter(formatter, *field_types) click to toggle source
# File lib/hammer_cli/output/output.rb, line 62
def self.register_formatter(formatter, *field_types)
  field_types.each do |type|
    formatter_list = formatters[type] || []
    formatter_list << formatter
    formatters[type] = formatter_list
  end
end

Public Instance Methods

adapter() click to toggle source
# File lib/hammer_cli/output/output.rb, line 37
def adapter
  adapter_name = context[:adapter] || default_adapter

  begin
    init_adapter(adapter_name.to_sym)
  rescue NameError
    Logging.logger[self.class.name].warn("Required adapter '#{adapter_name}' was not found, using 'base' instead")
    init_adapter(:base)
  end
end
print_collection(definition, collection) click to toggle source
print_error(msg, details=nil, msg_params = {}, options = {}) click to toggle source
print_message(msg, msg_params = {}, options = {}) click to toggle source
print_record(definition, record) click to toggle source

Protected Instance Methods

appropriate_verbosity?(msg_type, options = {}) click to toggle source
# File lib/hammer_cli/output/output.rb, line 72
def appropriate_verbosity?(msg_type, options = {})
  default = case msg_type
            when :message
              HammerCLI::V_VERBOSE
            when :error
              HammerCLI::V_QUIET
            when :record, :collection
              HammerCLI::V_UNIX
            end
  msg_verbosity = options[:verbosity] || default
  context[:verbosity] >= msg_verbosity
end

Private Instance Methods

init_adapter(adapter_name) click to toggle source
# File lib/hammer_cli/output/output.rb, line 89
def init_adapter(adapter_name)
  raise NameError unless self.class.adapters.has_key? adapter_name
  @adapter ||= self.class.adapters[adapter_name].new(context, self.class.formatters)
end