Class: HammerCLI::Output::Adapter::CSValues

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

Instance Method Summary collapse

Methods inherited from Abstract

#initialize, #print_error

Constructor Details

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

Instance Method Details



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hammer_cli/output/adapter/csv.rb', line 23

def print_collection(fields, collection)
  csv_string = generate do |csv|
    # labels
    csv << fields.select{ |f| !(f.class <= Fields::Id) || @context[:show_ids] }.map { |f| f.label }
    # data
    collection.each do |d|
      csv << fields.inject([]) do |row, f|
        unless f.class <= Fields::Id && !@context[:show_ids]
          value = (f.get_value(d) || '')
          formatter = @formatters.formatter_for_type(f.class)
          row << (formatter ? formatter.format(value) : value)
        end
        row
      end
    end
  end
  puts csv_string
end


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hammer_cli/output/adapter/csv.rb', line 42

def print_message(msg, msg_params={})
  csv_string = generate do |csv|
    id = msg_params["id"] || msg_params[:id]
    name = msg_params["name"] || msg_params[:name]

    labels = ["Message"]
    data = [msg.format(msg_params)]

    if id
      labels << "Id"
      data << id
    end

    if name
      labels << "Name"
      data << name
    end

    csv << labels
    csv << data
  end
  puts csv_string
end


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

def print_record(fields, record)
  print_collection(fields, [record].flatten(1))
end

#tagsObject



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

def tags
  [:flat]
end