Class: MonospaceTextFormatter::Box
- Inherits:
-
Object
- Object
- MonospaceTextFormatter::Box
- Defined in:
- lib/monospace_text_formatter/box.rb
Instance Attribute Summary collapse
-
#align ⇒ Object
Returns the value of attribute align.
-
#fill ⇒ Object
Returns the value of attribute fill.
-
#omission ⇒ Object
Returns the value of attribute omission.
-
#padding_bottom ⇒ Object
Returns the value of attribute padding_bottom.
-
#padding_left ⇒ Object
Returns the value of attribute padding_left.
-
#padding_right ⇒ Object
Returns the value of attribute padding_right.
-
#padding_top ⇒ Object
Returns the value of attribute padding_top.
-
#valign ⇒ Object
Returns the value of attribute valign.
Instance Method Summary collapse
- #attributes(attrs = {}) ⇒ Object
- #height ⇒ Object
- #height=(fixed_height) ⇒ Object
-
#initialize(string_or_chunk, attrs = {}) ⇒ Box
constructor
A new instance of Box.
- #inspect ⇒ Object
- #lines ⇒ Object
- #to_s ⇒ Object
- #truncated? ⇒ Boolean
- #width ⇒ Object
- #width=(fixed_width) ⇒ Object
Constructor Details
#initialize(string_or_chunk, attrs = {}) ⇒ Box
Returns a new instance of Box.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/monospace_text_formatter/box.rb', line 4 def initialize(string_or_chunk, attrs={}) @padding_top = 0 @padding_right = 0 @padding_bottom = 0 @padding_left = 0 @omission = string_to_chunk(" ...") @align = :left @valign = :top @fill = " " raise ArgumentError, "No string given" unless string_or_chunk @original_chunk = to_chunk_if_string(string_or_chunk) attributes(attrs) end |
Instance Attribute Details
#align ⇒ Object
Returns the value of attribute align.
22 23 24 |
# File 'lib/monospace_text_formatter/box.rb', line 22 def align @align end |
#fill ⇒ Object
Returns the value of attribute fill.
22 23 24 |
# File 'lib/monospace_text_formatter/box.rb', line 22 def fill @fill end |
#omission ⇒ Object
Returns the value of attribute omission.
22 23 24 |
# File 'lib/monospace_text_formatter/box.rb', line 22 def omission @omission end |
#padding_bottom ⇒ Object
Returns the value of attribute padding_bottom.
21 22 23 |
# File 'lib/monospace_text_formatter/box.rb', line 21 def padding_bottom @padding_bottom end |
#padding_left ⇒ Object
Returns the value of attribute padding_left.
21 22 23 |
# File 'lib/monospace_text_formatter/box.rb', line 21 def padding_left @padding_left end |
#padding_right ⇒ Object
Returns the value of attribute padding_right.
21 22 23 |
# File 'lib/monospace_text_formatter/box.rb', line 21 def padding_right @padding_right end |
#padding_top ⇒ Object
Returns the value of attribute padding_top.
21 22 23 |
# File 'lib/monospace_text_formatter/box.rb', line 21 def padding_top @padding_top end |
#valign ⇒ Object
Returns the value of attribute valign.
22 23 24 |
# File 'lib/monospace_text_formatter/box.rb', line 22 def valign @valign end |
Instance Method Details
#attributes(attrs = {}) ⇒ Object
32 33 34 |
# File 'lib/monospace_text_formatter/box.rb', line 32 def attributes(attrs={}) attrs.each { |name, value| send("#{name}=", value) } end |
#height ⇒ Object
28 29 30 |
# File 'lib/monospace_text_formatter/box.rb', line 28 def height @fixed_height || padding_top + content_lines.size + padding_bottom end |
#height=(fixed_height) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/monospace_text_formatter/box.rb', line 44 def height=(fixed_height) return if fixed_height == @fixed_height raise ArgumentError, "The :height must be equal or greater than 0, but is #{fixed_height}" unless fixed_height.nil? or fixed_height >= 0 @to_s = @lines = @aligned_all_lines = @all_lines = @empty_top_lines = @content_lines = @empty_bottom_lines = @fixed_height_minus_padding = nil @fixed_height = fixed_height end |
#inspect ⇒ Object
130 131 132 |
# File 'lib/monospace_text_formatter/box.rb', line 130 def inspect %Q(#<#{self.class} #{to_s.inspect}>) end |
#lines ⇒ Object
122 123 124 |
# File 'lib/monospace_text_formatter/box.rb', line 122 def lines @lines ||= aligned_all_lines.map { |aligned_line| aligned_line.to_s } end |
#to_s ⇒ Object
126 127 128 |
# File 'lib/monospace_text_formatter/box.rb', line 126 def to_s @to_s ||= lines.join("\n") end |
#truncated? ⇒ Boolean
118 119 120 |
# File 'lib/monospace_text_formatter/box.rb', line 118 def truncated? content_lines.last && content_lines.last.truncated? end |
#width ⇒ Object
24 25 26 |
# File 'lib/monospace_text_formatter/box.rb', line 24 def width @fixed_width || content_lines.first && content_lines.first.width end |
#width=(fixed_width) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/monospace_text_formatter/box.rb', line 36 def width=(fixed_width) return if fixed_width == @fixed_width raise ArgumentError, "The :width must be equal or greater than 0, but is #{fixed_width}" unless fixed_width.nil? or fixed_width >= 0 @to_s = @lines = @aligned_all_lines = @all_lines = @empty_top_lines = @content_lines = @empty_bottom_lines = @fixed_width_minus_padding = nil @fixed_width = fixed_width end |