Class: OutputMode::Outputs::Templated

Inherits:
OutputMode::Output show all
Defined in:
lib/output_mode/outputs/templated.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Attributes inherited from OutputMode::Output

#config, #context, #default, #no, #procs, #yes

Instance Method Summary collapse

Methods inherited from OutputMode::Output

#callables, #generate

Constructor Details

#initialize(*procs, template: nil, fields: nil, seperator: "\n", yes: 'true', no: 'false', **config) ⇒ Templated

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.

Parameters:

  • *procs (Array)
  • template: (ERB) (defaults to: nil)

    The template object used by the renderer

  • fields: (Array) (defaults to: nil)

    An optional array of field headers that map to the procs, repeating the last value if required

  • fields: (defaults to: nil)

    A static value to use as all field headers

  • separator:

    The character(s) used to join the “entries” together

  • colorize:

    Flags if the caller wants the colorized version, this maybe ignored by template

  • sections:

    An optional array that groups the procs into sections. This is ignored by default

  • **config (Hash)

See Also:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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

Instance Attribute Details

#colorizeObject (readonly)



89
# File 'lib/output_mode/outputs/templated.rb', line 89

attr_reader :erb, :fields, :separator, :colorize, :sections

#erbERB (readonly)

Returns The erb object containing the template to be rendered.

Returns:

  • (ERB)

    The erb object containing the template to be rendered.



89
90
91
# File 'lib/output_mode/outputs/templated.rb', line 89

def erb
  @erb
end

#fieldsObject (readonly)



89
# File 'lib/output_mode/outputs/templated.rb', line 89

attr_reader :erb, :fields, :separator, :colorize, :sections

#sectionsObject (readonly)

Returns the value of attribute sections.



89
# File 'lib/output_mode/outputs/templated.rb', line 89

attr_reader :erb, :fields, :separator, :colorize, :sections

#separatorObject (readonly)



89
# File 'lib/output_mode/outputs/templated.rb', line 89

attr_reader :erb, :fields, :separator, :colorize, :sections

Instance Method Details

#max_field_lengthObject

Returns the length of the maximum field



145
146
147
148
149
150
151
# 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) ⇒ Object

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



139
140
141
142
# 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