Class: MonospaceTextFormatter::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/monospace_text_formatter/box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_or_chunk, attrs = {}) ⇒ Box

Returns a new instance of Box.

Raises:

  • (ArgumentError)


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

#alignObject

Returns the value of attribute align.



22
23
24
# File 'lib/monospace_text_formatter/box.rb', line 22

def align
  @align
end

#fillObject

Returns the value of attribute fill.



22
23
24
# File 'lib/monospace_text_formatter/box.rb', line 22

def fill
  @fill
end

#omissionObject

Returns the value of attribute omission.



22
23
24
# File 'lib/monospace_text_formatter/box.rb', line 22

def omission
  @omission
end

#padding_bottomObject

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_leftObject

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_rightObject

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_topObject

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

#valignObject

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

#heightObject



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

Raises:

  • (ArgumentError)


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

#inspectObject



130
131
132
# File 'lib/monospace_text_formatter/box.rb', line 130

def inspect
  %Q(#<#{self.class} #{to_s.inspect}>)
end

#linesObject



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_sObject



126
127
128
# File 'lib/monospace_text_formatter/box.rb', line 126

def to_s
  @to_s ||= lines.join("\n")
end

#truncated?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/monospace_text_formatter/box.rb', line 118

def truncated?
  content_lines.last && content_lines.last.truncated?
end

#widthObject



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

Raises:

  • (ArgumentError)


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