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)

    DEPRECATED 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:

    DEPRECATED An optional array that groups the procs into sections

  • bind:

    An optional execution context to render the template within

  • **config (Hash)

See Also:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/output_mode/outputs/templated.rb', line 116

def initialize(*procs,
               template: nil,
               fields: nil,
               separator: "\n",
               colorize: false,
               sections: nil,
               bind: 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
  @bind = bind
  super(*procs, **config)
end

Instance Attribute Details

#bindObject (readonly)

Returns the value of attribute bind.



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

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

#colorizeObject (readonly)



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

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

#erbERB (readonly)

Returns The erb object containing the template to be rendered.

Returns:

  • (ERB)

    The erb object containing the template to be rendered.



92
93
94
# File 'lib/output_mode/outputs/templated.rb', line 92

def erb
  @erb
end

#fieldsObject (readonly)



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

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

#sectionsObject (readonly)



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

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

#separatorObject (readonly)



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

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

Instance Method Details

#max_field_lengthObject

Returns the length of the maximum field DEPRECATED



157
158
159
160
161
162
163
# File 'lib/output_mode/outputs/templated.rb', line 157

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



145
146
147
148
149
150
151
152
153
# File 'lib/output_mode/outputs/templated.rb', line 145

def render(*data)
  data.map do |datum|
    if bind
      erb.result(bind)
    else
      Entry.new(self, datum, colorize).render(erb)
    end
  end.join(separator)
end