class OutputMode::Outputs::Templated

Constants

Entry

Attributes

colorize[R]

@!attribute [r] erb

@return [ERB] The +erb+ object containing the template to be rendered.

@!attribute [r] separator @!attribute [r] fields @!attribute [r] colorize @!attribute [r] sections

erb[R]

@!attribute [r] erb

@return [ERB] The +erb+ object containing the template to be rendered.

@!attribute [r] separator @!attribute [r] fields @!attribute [r] colorize @!attribute [r] sections

fields[R]

@!attribute [r] erb

@return [ERB] The +erb+ object containing the template to be rendered.

@!attribute [r] separator @!attribute [r] fields @!attribute [r] colorize @!attribute [r] sections

sections[R]

@!attribute [r] erb

@return [ERB] The +erb+ object containing the template to be rendered.

@!attribute [r] separator @!attribute [r] fields @!attribute [r] colorize @!attribute [r] sections

separator[R]

@!attribute [r] erb

@return [ERB] The +erb+ object containing the template to be rendered.

@!attribute [r] separator @!attribute [r] fields @!attribute [r] colorize @!attribute [r] sections

Public Class Methods

new(*procs, template: nil, fields: nil, separator: "\n", colorize: false, sections: nil, **config) click to toggle source

Create a new output which will render using ERB. The provided template should only render the output for a single entry (aka model, record, data object, etc).

The template maybe either a String or a ERB object. Strings will automatically be converted to ERB with the trim_mode set to -.

A default template will be used if one has not be provided.

@see ruby-doc.org/stdlib-2.7.1/libdoc/erb/rdoc/ERB.html @see render @see DEFAULT_ERB

@overload initialize(*procs, template: nil, fields: nil, seperator: ā€œnā€, yes: 'true', no: 'false', **config)

@param [Array] *procs see {OutputMode::Output#initialize}
@param [ERB] template: The +template+ object used by the renderer
@param [Array] fields: An optional array of field headers that map to the procs, repeating the last value if required
@param fields: A static value to use as all field headers
@param separator: The character(s) used to join the "entries" together
@param colorize: Flags if the caller wants the colorized version, this maybe ignored by +template+
@param sections: An optional array that groups the procs into sections. This is ignored by default
@param [Hash] **config see {OutputMode::Output#initialize}
Calls superclass method OutputMode::Output::new
# File lib/output_mode/outputs/templated.rb, line 112
def initialize(*procs,
               template: nil,
               fields: nil,
               separator: "\n",
               colorize: false,
               sections: nil,
               **config)
  @erb = case template
  when String
    ERB.new(template, nil, '-')
  when ERB
    template
  else
    DEFAULT_ERB
  end
  @fields = fields
  @separator = separator
  @colorize = colorize
  @sections = sections
  super(*procs, **config)
end

Public Instance Methods

max_field_length() click to toggle source

Returns the length of the maximum field

# File lib/output_mode/outputs/templated.rb, line 145
def max_field_length
  if fields.is_a? Array
    fields.map { |f| f.to_s.length }.max
  else
    fields.to_s.length
  end
end
render(*data) click to toggle source

Implements the render method using the ERB template. The template will be rendered within the context of an Entry. An Entry object will be created/ rendered for each element of data

@see OutputMode::Output#render

# File lib/output_mode/outputs/templated.rb, line 139
def render(*data)
  data.map { |d| Entry.new(self, d, colorize).render(erb) }
      .join(separator)
end