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, #tags

Constructor Details

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

Returns a new instance of CSValues.



128
129
130
131
# File 'lib/hammer_cli/output/adapter/csv.rb', line 128

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

Instance Method Details

#featuresObject



133
134
135
136
137
# File 'lib/hammer_cli/output/adapter/csv.rb', line 133

def features
  return %i[serialized inline] if tags.empty?

  tags.map { |t| HammerCLI::Output::Utils.tag_to_feature(t) }
end


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

def print_collection(fields, collection, options = {})
  current_chunk = options[:current_chunk] || :single
  fields = filter_fields(fields).filter_by_classes
                                .filter_by_sets
                                .filter_by_data(collection.first,
                                                compact_only: true)
                                .filtered_fields
  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 && !@context[:no_headers] && %i[first single].include?(current_chunk)
    rows.each do |row|
      csv << Cell.values(headers, row)
    end
  end
  output_stream.puts csv_string
end


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

def print_message(msg, msg_params={})
  csv_string = generate do |csv|
    labels = [_("Message")]
    data = [msg.format(msg_params)]

    unless msg_params.nil?
      id = msg_params["id"] || msg_params[:id]
      name = msg_params["name"] || msg_params[:name]
      if id
        labels << _("Id")
        data << id
      end
      if name
        labels << _("Name")
        data << name
      end
    end

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


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

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

#row_data(fields, collection) ⇒ Object



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

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