class FormatOutput::BulletPointBuilder

A class to build bullet points for display.

Public Class Methods

new(options) click to toggle source

Prepare a blank slate.

# File lib/format_output/builders/bullet_builder.rb, line 10
def initialize(options)
  @body        = ::FormatOutput.width(options)
  @pad         = ::FormatOutput.pad(options)
  @bullet_data = []
  @key_length  = nil
end

Public Instance Methods

add(bullet, *items) click to toggle source

Add items to these bullet points.

# File lib/format_output/builders/bullet_builder.rb, line 18
def add(bullet, *items)
  items.each do |item|
    @bullet_data << [bullet.to_s, item]
    bullet = ""
  end
end
render() click to toggle source

Render the bullet points as an array of strings.

# File lib/format_output/builders/bullet_builder.rb, line 26
def render
  @key_length, results = get_key_length, []

  @bullet_data.each do |key, item|
    results.concat(render_bullet(key, item))
  end

  @bullet_data = []
  results
end

Private Instance Methods

get_key_length() click to toggle source

Allowing for a trailing space, how large is the largest bullet?

# File lib/format_output/builders/bullet_builder.rb, line 40
def get_key_length
  (@bullet_data.max_by {|line| line[0].length})[0].length + 1
end
render_bullet(key, item) click to toggle source

Render one bullet point. Returns: An array of strings, formatted as: bullet details

more details
more etc
# File lib/format_output/builders/bullet_builder.rb, line 48
def render_bullet(key, item)
  result = []

  item.format_output_bullet_detail(width: @body - @key_length - 1, left: 0).each do |desc_line|
    result << @pad + key.ljust(@key_length) + desc_line
    key = ""
  end

  result
end