Class: Berksfiler::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/berksfiler/formatter.rb

Overview

Methods for formatting data to be emplaced in a Berksfile

Class Method Summary collapse

Class Method Details

.aligned_print(lists) ⇒ Object

given a 2-dimensional array lists, right-pad each array member based on the maximum length of array members at that inner array index returns an array of formatted lines



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/berksfiler/formatter.rb', line 50

def self::aligned_print(lists)
  out = []
  maxes = array_maxes(lists)
  lists.each do |list|
    line = ''
    list.each_with_index do |value, index|
      line << "#{value.to_s.ljust(maxes[index])} "
    end
    out << line.strip
  end
  out
end

.array_maxes(lists) ⇒ Object

given a 2-dimensional array lists, return an array of the maximum length of the content for each index of the inner arrays



38
39
40
41
42
43
44
45
# File 'lib/berksfiler/formatter.rb', line 38

def self::array_maxes(lists)
  lists.reduce([]) do |maxes, list| # rubocop:disable Style/EachWithObject
    list.each_with_index do |value, index|
      maxes[index] = [(maxes[index] || 0), value.to_s.length].max
    end
    maxes
  end
end

.cookbook_line(cookbook) ⇒ Object

generate the correct Berksfile line for any cookbook



5
6
7
8
9
10
11
# File 'lib/berksfiler/formatter.rb', line 5

def self::cookbook_line(cookbook)
  if special_cookbook_lines.key?(cookbook)
    special_cookbook_lines[cookbook]
  else
    "cookbook '#{cookbook}'\n"
  end
end

.special_cookbook_linesObject

generate a hash of ‘name’ => ‘[formatted Berksfile line]’ for all local cookbooks and cookbooks that have specified options



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/berksfiler/formatter.rb', line 15

def self::special_cookbook_lines
  return @special_cookbook_lines if @special_cookbook_lines

  @special_cookbook_lines = {}

  # local cookbooks are discovered from coobooks_root, and are string names
  Berksfiler.local_cookbooks.map do |cb|
    @special_cookbook_lines[cb] = "cookbook '#{cb}', path: '../#{cb}'\n"
  end

  # cookbooks with options are provided as a hash
  # with name: and options: keys
  Berksfiler.cookbook_options.each do |cb|
    @special_cookbook_lines[cb['name']] =
      "cookbook '#{cb['name']}', #{cb['options']}\n"
  end
  @special_cookbook_lines
end