Class: HammerCLI::Output::Adapter::Json

Inherits:
TreeStructure show all
Defined in:
lib/hammer_cli/output/adapter/json.rb

Instance Method Summary collapse

Methods inherited from TreeStructure

#features, #initialize, #prepare_collection, #prepare_message

Methods inherited from Abstract

#features, #initialize, #paginate_by_default?, #print_error, #tags

Constructor Details

This class inherits a constructor from HammerCLI::Output::Adapter::TreeStructure

Instance Method Details



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hammer_cli/output/adapter/json.rb', line 9

def print_collection(fields, collection, options = {})
  current_chunk = options[:current_chunk] || :single
  prepared = prepare_collection(fields, collection)
  result = JSON.pretty_generate(prepared)
  if current_chunk != :single
    result = if current_chunk == :first
               result[0...-2] + ','
             elsif current_chunk == :last
               result[2..-1]
             else
               result[2...-2] + ','
             end
  end
  output_stream.puts result
end


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

def print_message(msg, msg_params={})
  data = prepare_message(msg, msg_params)
  puts JSON.pretty_generate(data)
end


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

def print_record(fields, record)
  result = prepare_collection(fields, [record].flatten(1))
  output_stream.puts JSON.pretty_generate(result.first)
end