Class: OutputMode::Formatters::Show

Inherits:
OutputMode::Formatter show all
Includes:
Enumerable
Defined in:
lib/output_mode/formatters/show.rb

Instance Method Summary collapse

Methods inherited from OutputMode::Formatter

#ascii?, build, #callables, #color?, #default, #format, #humanize?, #no, #register, #register_all, #render, render, #time, #verbose?, #yes

Constructor Details

#initialize(object, **opts) ⇒ Show

Limit the policy to a single object



36
37
38
# File 'lib/output_mode/formatters/show.rb', line 36

def initialize(object, **opts)
  super
end

Instance Method Details

#build_outputObject



54
55
56
57
58
59
60
61
# File 'lib/output_mode/formatters/show.rb', line 54

def build_output
  opts = {
    template: template,
    colorize: color?,
    bind: scope.instance_exec { binding }
  }
  OutputMode::Outputs::Templated.new(*callables, **opts)
end

#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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/output_mode/formatters/show.rb', line 67

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

  # Yield each selected attribute
  objs = selected.pad_each(object).map do |callable, opts|
    field = opts[:field]
    padding = opts[:padding]
    value = callable.call(object)
    [value, { field: field, padding: padding }]
  end

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

#objectObject



40
41
42
# File 'lib/output_mode/formatters/show.rb', line 40

def object
  @objects.first
end

#pastelObject

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



93
94
95
# File 'lib/output_mode/formatters/show.rb', line 93

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

#scope(value = nil) ⇒ Object



49
50
51
52
# File 'lib/output_mode/formatters/show.rb', line 49

def scope(value = nil)
  @scope = value unless value.nil?
  @scope ? @scope : self
end

#template(value = nil) ⇒ Object



44
45
46
47
# File 'lib/output_mode/formatters/show.rb', line 44

def template(value = nil)
  @template = value unless value.nil?
  @template ? @template : (humanize? ? DEFAULT_ERB : NON_INTERACTIVE_ERB)
end