Class: Ribosome::Block
- Inherits:
-
Object
- Object
- Ribosome::Block
- Defined in:
- lib/swift_generator/code_generation/swift_file_template.rb
Overview
Class Block represents a rectangular area of text.
Instance Attribute Summary collapse
-
#text ⇒ Object
Returns the value of attribute text.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#add_bottom(block) ⇒ Object
Weld the supplied block to the bottom side of this block.
-
#add_right(block) ⇒ Object
Weld the supplied block to the right side of this block.
-
#initialize(s) ⇒ Block
constructor
A new instance of Block.
-
#last_offset ⇒ Object
Returns offset of the last line in the block.
-
#trim ⇒ Object
Trim the whitespace from the block.
- #write(out, tabsize) ⇒ Object
Constructor Details
#initialize(s) ⇒ Block
Returns a new instance of Block.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 34 def initialize(s) @text = [] @width = 0 return if s == nil # Split the string into individual lines. start = 0 loop do i = s.index("\n", start) || s.size @text << (i == start ? "" : s[start..i - 1]) @width = [@width, @text.last.size].max start = i + 1 break if start > s.size end end |
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
32 33 34 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 32 def text @text end |
#width ⇒ Object
Returns the value of attribute width.
32 33 34 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 32 def width @width end |
Instance Method Details
#add_bottom(block) ⇒ Object
Weld the supplied block to the bottom side of this block.
71 72 73 74 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 71 def add_bottom(block) @text += block.text @width = [@width, block.width].max end |
#add_right(block) ⇒ Object
Weld the supplied block to the right side of this block.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 51 def add_right(block) # Merge the blocks while taking care to add whitespace # where they do not align properly. i = 0 for l in block.text if(@text[i]) @text[i] += (" " * (@width - @text[i].size)) + l else @text << (" " * @width) + l end i += 1 end # Adjust the overall width of the block. @width += block.width end |
#last_offset ⇒ Object
Returns offset of the last line in the block.
142 143 144 145 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 142 def last_offset() return 0 if @text.empty? return @text.last.size - @text.last.lstrip.size end |
#trim ⇒ Object
Trim the whitespace from the block.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 77 def trim() # Find the boundaries of the text. top = -1 bottom = -1 left = -1 right = -1 i = 0 for l in @text if(!l.lstrip().empty?) top = i if top == -1 bottom = i; if (left == -1) left = l.size() - l.lstrip().size() else left = [left, l.size() - l.lstrip().size()].min end if (right == -1) right = l.rstrip().size() else right = [right, l.rstrip().size()].max end end i += 1 end # The case of block with no text whatsoever. if bottom == -1 @text = [] @width = 0 return end # Strip off the top and bottom whitespace. @text = @text[top..bottom] # Strip off the whitespace on the left and on the right. for i in 0..@text.size() - 1 @text[i] = @text[i].rstrip()[left..right] @text[i] = "" if @text[i] == nil end # Adjust the overall width of the block. @width = (@text.max {|x,y| x.size <=> y.size} || "").size end |
#write(out, tabsize) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 125 def write(out, tabsize) for l in @text # If required, replace the initial whitespace by tabs. if(tabsize > 0) ws = l.size - l.lstrip.size l = "\t" * (ws / tabsize) + " " * (ws % tabsize) + l.lstrip end # Write an individual line to the output file. out.write(l) out.write("\n") end end |