class HammerCLI::Output::Adapter::CSValues
Public Class Methods
new(context={}, formatters={})
click to toggle source
Calls superclass method
HammerCLI::Output::Adapter::Abstract.new
# File lib/hammer_cli/output/adapter/csv.rb, line 129 def initialize(context={}, formatters={}) super @paginate_by_default = false end
Public Instance Methods
print_collection(fields, collection)
click to toggle source
# File lib/hammer_cli/output/adapter/csv.rb, line 150 def print_collection(fields, collection) fields = displayable_fields(fields, collection.first, compact_only: true) 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] rows.each do |row| csv << Cell.values(headers, row) end end output_stream.puts csv_string end
print_message(msg, msg_params={})
click to toggle source
# File lib/hammer_cli/output/adapter/csv.rb, line 166 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
print_record(fields, record)
click to toggle source
# File lib/hammer_cli/output/adapter/csv.rb, line 146 def print_record(fields, record) print_collection(fields, [record].flatten(1)) end
row_data(fields, collection)
click to toggle source
# File lib/hammer_cli/output/adapter/csv.rb, line 138 def row_data(fields, collection) result = [] collection.each do |data| result << Cell.create_cells(FieldWrapper.wrap(fields), data, @formatters) end result end
Private Instance Methods
default_headers(fields)
click to toggle source
# File lib/hammer_cli/output/adapter/csv.rb, line 200 def default_headers(fields) fields.select{ |f| !(f.class <= Fields::Id) || @context[:show_ids] }.map { |f| f.label } end
generate(&block)
click to toggle source
# File lib/hammer_cli/output/adapter/csv.rb, line 192 def generate(&block) CSV.generate( :col_sep => @context[:csv_separator] || ',', :encoding => 'utf-8', &block ) end