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

#paginate_by_default?, #print_error

Constructor Details

#initialize(context = {}, formatters = {}) ⇒ CSValues

Returns a new instance of CSValues.



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

def initialize(context={}, formatters={})
  super
  @paginate_by_default = false
end

Instance Method Details



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hammer_cli/output/adapter/csv.rb', line 159

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


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/hammer_cli/output/adapter/csv.rb', line 174

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


155
156
157
# File 'lib/hammer_cli/output/adapter/csv.rb', line 155

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

#row_data(fields, collection) ⇒ Object



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

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

#tagsObject



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

def tags
  [:flat]
end