Class: OutputMode::Outputs::Templated
- Inherits:
-
OutputMode::Output
- Object
- OutputMode::Output
- OutputMode::Outputs::Templated
- Defined in:
- lib/output_mode/outputs/templated.rb
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#bind ⇒ Object
readonly
Returns the value of attribute bind.
- #colorize ⇒ Object readonly
-
#erb ⇒ ERB
readonly
The
erb
object containing the template to be rendered. - #fields ⇒ Object readonly
- #sections ⇒ Object readonly
- #separator ⇒ Object readonly
Attributes inherited from OutputMode::Output
#config, #context, #default, #no, #procs, #yes
Instance Method Summary collapse
-
#initialize(*procs, template: nil, fields: nil, seperator: "\n", yes: 'true', no: 'false', **config) ⇒ Templated
constructor
Create a new
output
which will render usingERB
. -
#max_field_length ⇒ Object
Returns the length of the maximum field DEPRECATED.
-
#render(*data) ⇒ Object
Implements the render method using the ERB
template
.
Methods inherited from OutputMode::Output
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.
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
#bind ⇒ Object (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 |
#colorize ⇒ Object (readonly)
92 |
# File 'lib/output_mode/outputs/templated.rb', line 92 attr_reader :erb, :fields, :separator, :colorize, :sections, :bind |
#erb ⇒ ERB (readonly)
Returns 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 |
#fields ⇒ Object (readonly)
92 |
# File 'lib/output_mode/outputs/templated.rb', line 92 attr_reader :erb, :fields, :separator, :colorize, :sections, :bind |
#sections ⇒ Object (readonly)
92 |
# File 'lib/output_mode/outputs/templated.rb', line 92 attr_reader :erb, :fields, :separator, :colorize, :sections, :bind |
#separator ⇒ Object (readonly)
92 |
# File 'lib/output_mode/outputs/templated.rb', line 92 attr_reader :erb, :fields, :separator, :colorize, :sections, :bind |
Instance Method Details
#max_field_length ⇒ Object
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 |