Class: HammerCLI::Output::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/output/output.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}, options = {}) ⇒ Output

Returns a new instance of Output.



10
11
12
13
14
# 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

Instance Attribute Details

#default_adapterObject

Returns the value of attribute default_adapter.



16
17
18
# File 'lib/hammer_cli/output/output.rb', line 16

def default_adapter
  @default_adapter
end

Class Method Details

.adaptersObject



48
49
50
51
# File 'lib/hammer_cli/output/output.rb', line 48

def self.adapters
  @adapters_hash ||= {}
  @adapters_hash
end

.formattersObject



53
54
55
56
# File 'lib/hammer_cli/output/output.rb', line 53

def self.formatters
  @formatters_hash ||= {}
  @formatters_hash
end

.register_adapter(name, adapter_class) ⇒ Object



58
59
60
# 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) ⇒ Object



62
63
64
65
66
67
68
# 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

Instance Method Details

#adapterObject



37
38
39
40
41
42
43
44
45
46
# 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


30
31
32
33
34
35
# File 'lib/hammer_cli/output/output.rb', line 30

def print_collection(definition, collection, options = {})
  unless collection.class <= HammerCLI::Output::RecordCollection
    collection = HammerCLI::Output::RecordCollection.new([collection].flatten(1))
  end
  adapter.print_collection(definition.fields, collection, options) if appropriate_verbosity?(:collection)
end


22
23
24
# File 'lib/hammer_cli/output/output.rb', line 22

def print_error(msg, details=nil, msg_params = {}, options = {})
  adapter.print_error(msg.to_s, details, msg_params) if appropriate_verbosity?(:error, options)
end


18
19
20
# File 'lib/hammer_cli/output/output.rb', line 18

def print_message(msg, msg_params = {}, options = {})
  adapter.print_message(msg.to_s, msg_params) if appropriate_verbosity?(:message, options)
end


26
27
28
# File 'lib/hammer_cli/output/output.rb', line 26

def print_record(definition, record)
  adapter.print_record(definition.fields, record) if appropriate_verbosity?(:record)
end