Class: MonospaceTextFormatter::Line

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Line.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/monospace_text_formatter/line.rb', line 4

def initialize(string_or_chunk, attrs={})
  @padding_left  = 0
  @padding_right = 0
       @omission = string_to_chunk(" ...")
          @align = :left
           @fill = " "
      @truncated = nil

  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.



18
19
20
# File 'lib/monospace_text_formatter/line.rb', line 18

def align
  @align
end

#fillObject

Returns the value of attribute fill.



18
19
20
# File 'lib/monospace_text_formatter/line.rb', line 18

def fill
  @fill
end

#omissionObject

Returns the value of attribute omission.



18
19
20
# File 'lib/monospace_text_formatter/line.rb', line 18

def omission
  @omission
end

#padding_leftObject

Returns the value of attribute padding_left.



18
19
20
# File 'lib/monospace_text_formatter/line.rb', line 18

def padding_left
  @padding_left
end

#padding_rightObject

Returns the value of attribute padding_right.



18
19
20
# File 'lib/monospace_text_formatter/line.rb', line 18

def padding_right
  @padding_right
end

Instance Method Details

#attributes(attrs = {}) ⇒ Object



24
25
26
# File 'lib/monospace_text_formatter/line.rb', line 24

def attributes(attrs={})
  attrs.each { |name, value| send("#{name}=", value) }
end

#inspectObject



85
86
87
# File 'lib/monospace_text_formatter/line.rb', line 85

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

#to_sObject



81
82
83
# File 'lib/monospace_text_formatter/line.rb', line 81

def to_s
  aligned_visible_text
end

#truncated?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/monospace_text_formatter/line.rb', line 76

def truncated?
  visible_chunk
  @truncated
end

#widthObject



20
21
22
# File 'lib/monospace_text_formatter/line.rb', line 20

def width
  @fixed_width || padding_left + visible_chunk.display_length + padding_right
end

#width=(fixed_width) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
# File 'lib/monospace_text_formatter/line.rb', line 28

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

  @aligned_visible_text = @visible_chunk = @fixed_width_minus_padding = nil
  @fixed_width = fixed_width
end