Class: BoxBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/boxybox/models/box_builder.rb

Overview

helper methods to create and merge terminal boxes

Author:

  • Travis Carter

Constant Summary collapse

SPACE =
' '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BoxBuilder

Returns new instance of BoxBuilder.

Parameters:

  • config (Object)


15
16
17
18
19
# File 'lib/boxybox/models/box_builder.rb', line 15

def initialize(config)
  @direction = config['direction']
  @children = config['children']
  @content = config['content']
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



11
12
13
# File 'lib/boxybox/models/box_builder.rb', line 11

def children
  @children
end

#contentObject

Returns the value of attribute content.



11
12
13
# File 'lib/boxybox/models/box_builder.rb', line 11

def content
  @content
end

#directionObject

Returns the value of attribute direction.



11
12
13
# File 'lib/boxybox/models/box_builder.rb', line 11

def direction
  @direction
end

Class Method Details

.create_from_json(json_string) ⇒ String

This kicks off the parsing of the input JSON and returns the nested boxes as a string

Parameters:

  • json_string (JSON)

    input to create boxes

Returns:

  • (String)

    will return a multiline string of ascii boxes



24
25
26
27
28
29
# File 'lib/boxybox/models/box_builder.rb', line 24

def self.create_from_json(json_string)
  config = JSON.parse(json_string)
  box_builder = BoxBuilder.new(config['boxes'])

  box_builder.create_boxes
end

.merge_boxes(*box_strings) ⇒ String

Parameters:

  • box_strings (Array)

    boxes to be merged

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/boxybox/models/box_builder.rb', line 58

def self.merge_boxes(*box_strings)
  box_arrays = {}
  max_rows = 0
  new_box = []

  box_strings.each_with_index do |box, idx|
    box_arrays[idx] = box.split("\n")

    max_rows = max_rows > box_arrays[idx].length ? max_rows : box_arrays[idx].length
  end

  max_rows.times do |x|
    new_line = ''
    box_arrays.each_value { |box| new_line += box[x].nil? ? SPACE * box.first.length : box[x] }

    new_box << new_line
  end

  new_box.join("\n")
end

Instance Method Details

#create_boxesStrings

This is the recursive method that craws down the tree to create the boxes

Returns:

  • (Strings)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/boxybox/models/box_builder.rb', line 33

def create_boxes
  unless @children.nil?
    child_boxes = []

    children.each { |child| child_boxes << BoxBuilder.new(child).create_boxes }

    @content = if child_boxes.length > 1 && direction == 'horizontal'
                 BoxBuilder.merge_boxes(*child_boxes)
               else
                 child_boxes.join("\n")
               end
  end

  @content = make_box(@content)
end

#make_box(content) ⇒ Strings

Wrapper around the TTY::Box module. This will be a prime target for expanded style choices and using more of the customizations available within the TTY gem set

Returns:

  • (Strings)

    ascii box



52
53
54
# File 'lib/boxybox/models/box_builder.rb', line 52

def make_box(content)
  TTY::Box.frame(content, padding: 1, border: :ascii)
end