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.



4
5
6
7
# File 'lib/hammer_cli/output/output.rb', line 4

def initialize(context={}, options={})
  self.context = context
  self.default_adapter = options[:default_adapter]
end

Instance Attribute Details

#default_adapterObject

Returns the value of attribute default_adapter.



9
10
11
# File 'lib/hammer_cli/output/output.rb', line 9

def default_adapter
  @default_adapter
end

Class Method Details

.adaptersObject



41
42
43
44
# File 'lib/hammer_cli/output/output.rb', line 41

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

.formattersObject



46
47
48
49
# File 'lib/hammer_cli/output/output.rb', line 46

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

.register_adapter(name, adapter_class) ⇒ Object



51
52
53
# File 'lib/hammer_cli/output/output.rb', line 51

def self.register_adapter(name, adapter_class)
  adapters[name] = adapter_class
end

.register_formatter(formatter, *field_types) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/hammer_cli/output/output.rb', line 55

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



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

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


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

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


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

def print_error(msg, details=nil, msg_params={})
  adapter.print_error(msg.to_s, details, msg_params)
end


11
12
13
# File 'lib/hammer_cli/output/output.rb', line 11

def print_message(msg, msg_params={})
  adapter.print_message(msg.to_s, msg_params)
end


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

def print_record(definition, record)
  adapter.print_record(definition.fields, record)
end