class HammerCLI::Output::Adapter::Base

Constants

GROUP_INDENT
LABEL_DIVIDER

Public Instance Methods

print_collection(fields, collection) click to toggle source
print_record(fields, record) click to toggle source
tags() click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 7
def tags
  [:flat, :screen]
end

Protected Instance Methods

field_filter() click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 24
def field_filter
  filtered = []
  filtered << Fields::Id unless @context[:show_ids]
  HammerCLI::Output::FieldFilter.new(filtered)
end
filter_fields(fields, data) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 30
def filter_fields(fields, data)
  field_filter.filter(fields).reject do |field|
    field_data = data_for_field(field, data)
    not field.display?(field_data)
  end
end
label_width(fields) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 94
def label_width(fields)
  fields.inject(0) do |result, f|
    width = f.label.to_s.size + LABEL_DIVIDER.size
    (width > result) ? width : result
  end
end
render_field(field, data, label_width) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 54
def render_field(field, data, label_width)
  if field.is_a? Fields::ContainerField
    output = ""

    idx = 0
    data = [data] unless data.is_a? Array
    data.each do |d|
      idx += 1
      fields_output = render_fields(field.fields, d).indent_with(GROUP_INDENT)
      if field.is_a?(Fields::Collection) && field.parameters[:numbered]
        fields_output = fields_output.sub(/^[ ]{4}/, " %-3s" % "#{idx})")
      end

      output += fields_output
      output += "\n"
    end

    render_label(field, label_width) + "\n" + output.rstrip
  else
    render_label(field, label_width) +
    render_value(field, data)
  end
end
render_fields(fields, data) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 37
def render_fields(fields, data)
  output = ""

  fields = filter_fields(fields, data)

  label_width = label_width(fields)

  fields.collect do |field|
    field_data = data_for_field(field, data)

    next unless field.display?(field_data)
    output += render_field(field, field_data, label_width)
    output += "\n"
  end
  output.rstrip
end
render_label(field, width) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 78
def render_label(field, width)
  if field.label
    "%-#{width}s" % (field.label.to_s + LABEL_DIVIDER)
  else
    ""
  end
end
render_value(field, data) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 86
def render_value(field, data)
  formatter = @formatters.formatter_for_type(field.class)
  parameters = field.parameters
  parameters[:context] = @context
  data = formatter.format(data, field.parameters) if formatter
  data.to_s
end