Class: BoxBuilder
- Inherits:
-
Object
- Object
- BoxBuilder
- Defined in:
- lib/boxybox/models/box_builder.rb
Overview
helper methods to create and merge terminal boxes
Constant Summary collapse
- SPACE =
' '
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#content ⇒ Object
Returns the value of attribute content.
-
#direction ⇒ Object
Returns the value of attribute direction.
Class Method Summary collapse
-
.create_from_json(json_string) ⇒ String
This kicks off the parsing of the input JSON and returns the nested boxes as a string.
- .merge_boxes(*box_strings) ⇒ String
Instance Method Summary collapse
-
#create_boxes ⇒ Strings
This is the recursive method that craws down the tree to create the boxes.
-
#initialize(config) ⇒ BoxBuilder
constructor
New instance of BoxBuilder.
-
#make_box(content) ⇒ Strings
Wrapper around the TTY::Box module.
Constructor Details
#initialize(config) ⇒ BoxBuilder
Returns new instance of BoxBuilder.
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
#children ⇒ Object
Returns the value of attribute children.
11 12 13 |
# File 'lib/boxybox/models/box_builder.rb', line 11 def children @children end |
#content ⇒ Object
Returns the value of attribute content.
11 12 13 |
# File 'lib/boxybox/models/box_builder.rb', line 11 def content @content end |
#direction ⇒ Object
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
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
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_boxes ⇒ Strings
This is the recursive method that craws down the tree to create the boxes
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
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 |