Class: HammerCLI::Output::Output
- Inherits:
-
Object
- Object
- HammerCLI::Output::Output
- Defined in:
- lib/hammer_cli/output/output.rb
Instance Attribute Summary collapse
-
#default_adapter ⇒ Object
Returns the value of attribute default_adapter.
Class Method Summary collapse
- .adapters ⇒ Object
- .formatters ⇒ Object
- .register_adapter(name, adapter_class) ⇒ Object
- .register_formatter(formatter, *field_types) ⇒ Object
Instance Method Summary collapse
- #adapter ⇒ Object
-
#initialize(context = {}, options = {}) ⇒ Output
constructor
A new instance of Output.
- #print_collection(definition, collection) ⇒ Object
- #print_error(msg, details = nil, msg_params = {}) ⇒ Object
- #print_message(msg, msg_params = {}) ⇒ Object
- #print_record(definition, record) ⇒ Object
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={}, ={}) self.context = context self.default_adapter = [:default_adapter] end |
Instance Attribute Details
#default_adapter ⇒ Object
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
.adapters ⇒ Object
41 42 43 44 |
# File 'lib/hammer_cli/output/output.rb', line 41 def self.adapters @adapters_hash ||= {} @adapters_hash end |
.formatters ⇒ Object
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
#adapter ⇒ Object
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 |
#print_collection(definition, collection) ⇒ Object
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 |
#print_error(msg, details = nil, msg_params = {}) ⇒ Object
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 |
#print_message(msg, msg_params = {}) ⇒ Object
11 12 13 |
# File 'lib/hammer_cli/output/output.rb', line 11 def (msg, msg_params={}) adapter.(msg.to_s, msg_params) end |
#print_record(definition, record) ⇒ Object
19 20 21 |
# File 'lib/hammer_cli/output/output.rb', line 19 def print_record(definition, record) adapter.print_record(definition.fields, record) end |