Class: OutputMode::Outputs::Templated::Entry

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/output_mode/outputs/templated.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorizeObject

Returns the value of attribute colorize

Returns:

  • (Object)

    the current value of colorize



34
35
36
# File 'lib/output_mode/outputs/templated.rb', line 34

def colorize
  @colorize
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



34
35
36
# File 'lib/output_mode/outputs/templated.rb', line 34

def model
  @model
end

#outputObject

Returns the value of attribute output

Returns:

  • (Object)

    the current value of output



34
35
36
# File 'lib/output_mode/outputs/templated.rb', line 34

def output
  @output
end

Instance Method Details

#each(section = nil) {|value, field:, padding:, **config| ... } ⇒ Object

Yield Parameters:

  • value

    An attribute to be rendered

  • field:

    An optional field header for the value

  • padding:

    A padding string which will right align the field

  • **config

    TBA



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/output_mode/outputs/templated.rb', line 41

def each(section = nil, &block)
  # Select the callable objects
  callables = if section == nil
                output.callables
              elsif section == :default
                output.callables.config_select(:section, :default, nil)
              else
                output.callables.config_select(:section, section)
              end

  # Yield each selected attribute
  objs = callables.pad_each(model, **output.context).map do |callable, padding:, field:|
    value = callable.generator(output).call(model)
    [value, { field: field, padding: padding }]
  end

  # Runs the provided block
  objs.each do |model, opts|
    block.call(model, **opts)
  end
end

#pastelObject

Library for colorizing the output. It is automatically disabled when the colorize flag is false



74
75
76
# File 'lib/output_mode/outputs/templated.rb', line 74

def pastel
  @pastel ||= Pastel.new(enabled: colorize)
end

#render(erb) ⇒ String

Renders an ERB object within the entry’s context. This provides access to the output, model, and enumerable methods

Parameters:

  • erb (ERB)

    the ERB object which contains the template to be rendered

Returns:

  • (String)

    the result text



68
69
70
# File 'lib/output_mode/outputs/templated.rb', line 68

def render(erb)
  erb.result(binding)
end