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

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

Defined Under Namespace

Classes: Cell, FieldWrapper

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/hammer_cli/output/adapter/csv.rb', line 154

def print_collection(fields, collection)
  rows = row_data(fields, collection)
  # get headers using columns heuristic
  headers = rows.map{ |r| Cell.headers(r, @context) }.max_by{ |headers| headers.size }
  # or use headers from output definition
  headers ||= default_headers(fields)
  csv_string = generate do |csv|
    csv << headers if headers
    rows.each do |row|
      csv << Cell.values(headers, row)
    end
  end
  puts csv_string
end


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/hammer_cli/output/adapter/csv.rb', line 169

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


150
151
152
# File 'lib/hammer_cli/output/adapter/csv.rb', line 150

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

#row_data(fields, collection) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/hammer_cli/output/adapter/csv.rb', line 142

def row_data(fields, collection)
  result = []
  collection.each do |data|
    result << Cell.create_cells(FieldWrapper.wrap(fields), data, @formatters)
  end
  result
end

#tagsObject



138
139
140
# File 'lib/hammer_cli/output/adapter/csv.rb', line 138

def tags
  [:flat]
end