Class: BenchmarkSpec::OutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark_spec/output_formatter.rb

Constant Summary collapse

TITLE_LENGHT =
100
TITLE_FILLER =
'-'
MIN_NUM_OF_FILLERS =
20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent_level: 0, space_per_indent: 2) ⇒ OutputFormatter

Returns a new instance of OutputFormatter.



9
10
11
12
# File 'lib/benchmark_spec/output_formatter.rb', line 9

def initialize(indent_level: 0, space_per_indent: 2)
  @indent_level = indent_level
  @space_per_indent = space_per_indent
end

Instance Attribute Details

#indent_levelObject

Returns the value of attribute indent_level.



3
4
5
# File 'lib/benchmark_spec/output_formatter.rb', line 3

def indent_level
  @indent_level
end

#space_per_indentObject

Returns the value of attribute space_per_indent.



3
4
5
# File 'lib/benchmark_spec/output_formatter.rb', line 3

def space_per_indent
  @space_per_indent
end

Class Method Details

.new_lineObject



30
31
32
# File 'lib/benchmark_spec/output_formatter.rb', line 30

def new_line
  puts "\n"
end

.title(content) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/benchmark_spec/output_formatter.rb', line 15

def title(content)
  with_margins_vertically do
    side_fillers =
      TITLE_FILLER * ([TITLE_LENGHT - content.length, MIN_NUM_OF_FILLERS].max / 2 - 1) # -1 for the space

    puts "#{side_fillers} #{content} #{side_fillers}"
  end
end

.with_margins_vertically(num_of_lines = 1) ⇒ Object



24
25
26
27
28
# File 'lib/benchmark_spec/output_formatter.rb', line 24

def with_margins_vertically(num_of_lines = 1)
  num_of_lines.times { new_line }
  yield
  num_of_lines.times { new_line }
end

Instance Method Details

#indent_message(message) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/benchmark_spec/output_formatter.rb', line 52

def indent_message(message)
  return message unless indent_level > 0

  message = message.gsub("\n", "\n" + margin)

  return "#{margin}#{message}"
end

#marginObject



60
61
62
# File 'lib/benchmark_spec/output_formatter.rb', line 60

def margin
  " " * indent_level * space_per_indent
end

#new_lineObject



39
40
41
# File 'lib/benchmark_spec/output_formatter.rb', line 39

def new_line
  self.class.new_line
end


47
48
49
50
# File 'lib/benchmark_spec/output_formatter.rb', line 47

def print(message)
  message = indent_message(message)
  puts message
end

#title(content) ⇒ Object



35
36
37
# File 'lib/benchmark_spec/output_formatter.rb', line 35

def title(content)
  self.class.title(content)
end

#with_margins_vertically(*a, &b) ⇒ Object



43
44
45
# File 'lib/benchmark_spec/output_formatter.rb', line 43

def with_margins_vertically(*a, &b)
  self.class.with_margins_vertically(*a, &b)
end