class HammerCLI::Output::Adapter::Base

Constants

GROUP_INDENT
LABEL_DIVIDER

Public Instance Methods

features() click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 7
def features
  return %i[serialized rich_text multiline] if tags.empty?

  tags.map { |t| HammerCLI::Output::Utils.tag_to_feature(t) }
end
print_collection(fields, collection, options = {}) click to toggle source
print_record(fields, record) click to toggle source

Protected Instance Methods

label_width(fields) click to toggle source
# File lib/hammer_cli/output/adapter/base.rb, line 84
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 44
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 26
def render_fields(fields, data)
  output = ''
  fields = filter_fields(fields).filter_by_classes
                                .filter_by_sets
                                .filter_by_data(data)
                                .filtered_fields
  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 68
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 76
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