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
- #colorize ⇒ Object readonly
-
#erb ⇒ ERB
readonly
The
erbobject containing the template to be rendered. - #fields ⇒ Object readonly
-
#sections ⇒ Object
readonly
Returns the value of attribute sections.
- #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
outputwhich will render usingERB. -
#max_field_length ⇒ Object
Returns the length of the maximum field.
-
#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.
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
#colorize ⇒ Object (readonly)
89 |
# File 'lib/output_mode/outputs/templated.rb', line 89 attr_reader :erb, :fields, :separator, :colorize, :sections |
#erb ⇒ ERB (readonly)
Returns 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 |
#fields ⇒ Object (readonly)
89 |
# File 'lib/output_mode/outputs/templated.rb', line 89 attr_reader :erb, :fields, :separator, :colorize, :sections |
#sections ⇒ Object (readonly)
Returns the value of attribute sections.
89 |
# File 'lib/output_mode/outputs/templated.rb', line 89 attr_reader :erb, :fields, :separator, :colorize, :sections |
#separator ⇒ Object (readonly)
89 |
# File 'lib/output_mode/outputs/templated.rb', line 89 attr_reader :erb, :fields, :separator, :colorize, :sections |
Instance Method Details
#max_field_length ⇒ Object
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 |