Class: Wrapomatic::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/wrapomatic/line.rb,
lib/wrapomatic/line/processor.rb,
lib/wrapomatic/line/processor/base.rb,
lib/wrapomatic/line/processor/primary.rb,
lib/wrapomatic/line/processor/remainder.rb

Overview

Wrap a single line

Defined Under Namespace

Modules: Processor

Constant Summary collapse

INDENTATION =

An indentation in Wrapomatic is two spaces

'  '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original, indents, columns) ⇒ Line

Returns a new instance of Line.

Parameters:

  • original (String)

    a single line of text

  • indents (Integer)

    the number of indentation levels to which the line should be wrapped

  • columns (Integer)

    the number of columns to which the line should be wrapped

Raises:

  • (ArgumentError)

    if the provided text contains newline characters



30
31
32
33
# File 'lib/wrapomatic/line.rb', line 30

def initialize(original, indents, columns)
  @original, @indents, @columns = original, indents, columns
  raise ArgumentError.new('original may not contain newline') if contains_newline?
end

Instance Attribute Details

#columnsInteger (readonly)

Returns the columns cutoff.

Returns:

  • (Integer)

    the columns cutoff



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wrapomatic/line.rb', line 15

class Line
  # An indentation in Wrapomatic is two spaces
  INDENTATION = '  '

  attr_reader :original, :indents, :columns

  # @param original [String] a single line of text
  #
  # @param indents [Integer] the number of indentation levels to which the
  #   line should be wrapped
  #
  # @param columns [Integer] the number of columns to which the line should be
  #   wrapped
  #
  # @raise [ArgumentError] if the provided text contains newline characters
  def initialize(original, indents, columns)
    @original, @indents, @columns = original, indents, columns
    raise ArgumentError.new('original may not contain newline') if contains_newline?
  end

  # The result of wrapping the text to the provided indentation level and
  # columns
  #
  # @return [Array<String>] the original line broken into several lines that
  #   adhere to the column cutoff and indentation level
  def wrapped
    @wrapped ||= indented.length <= columns ? 
      [indented] :
      Processor.process(self)
  end

  # The indented line before wrapping
  #
  # @return [String] the line indented to the indentation level
  def indented
    "#{INDENTATION * indents}#{original}"
  end

  private
  def contains_newline?
    original.chars.include?("\n")
  end
end

#indentsInteger (readonly)

Returns the indentation level.

Returns:

  • (Integer)

    the indentation level



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wrapomatic/line.rb', line 15

class Line
  # An indentation in Wrapomatic is two spaces
  INDENTATION = '  '

  attr_reader :original, :indents, :columns

  # @param original [String] a single line of text
  #
  # @param indents [Integer] the number of indentation levels to which the
  #   line should be wrapped
  #
  # @param columns [Integer] the number of columns to which the line should be
  #   wrapped
  #
  # @raise [ArgumentError] if the provided text contains newline characters
  def initialize(original, indents, columns)
    @original, @indents, @columns = original, indents, columns
    raise ArgumentError.new('original may not contain newline') if contains_newline?
  end

  # The result of wrapping the text to the provided indentation level and
  # columns
  #
  # @return [Array<String>] the original line broken into several lines that
  #   adhere to the column cutoff and indentation level
  def wrapped
    @wrapped ||= indented.length <= columns ? 
      [indented] :
      Processor.process(self)
  end

  # The indented line before wrapping
  #
  # @return [String] the line indented to the indentation level
  def indented
    "#{INDENTATION * indents}#{original}"
  end

  private
  def contains_newline?
    original.chars.include?("\n")
  end
end

#originalString (readonly)

Returns the original text.

Returns:

  • (String)

    the original text



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/wrapomatic/line.rb', line 15

class Line
  # An indentation in Wrapomatic is two spaces
  INDENTATION = '  '

  attr_reader :original, :indents, :columns

  # @param original [String] a single line of text
  #
  # @param indents [Integer] the number of indentation levels to which the
  #   line should be wrapped
  #
  # @param columns [Integer] the number of columns to which the line should be
  #   wrapped
  #
  # @raise [ArgumentError] if the provided text contains newline characters
  def initialize(original, indents, columns)
    @original, @indents, @columns = original, indents, columns
    raise ArgumentError.new('original may not contain newline') if contains_newline?
  end

  # The result of wrapping the text to the provided indentation level and
  # columns
  #
  # @return [Array<String>] the original line broken into several lines that
  #   adhere to the column cutoff and indentation level
  def wrapped
    @wrapped ||= indented.length <= columns ? 
      [indented] :
      Processor.process(self)
  end

  # The indented line before wrapping
  #
  # @return [String] the line indented to the indentation level
  def indented
    "#{INDENTATION * indents}#{original}"
  end

  private
  def contains_newline?
    original.chars.include?("\n")
  end
end

Instance Method Details

#indentedString

The indented line before wrapping

Returns:

  • (String)

    the line indented to the indentation level



49
50
51
# File 'lib/wrapomatic/line.rb', line 49

def indented
  "#{INDENTATION * indents}#{original}"
end

#wrappedArray<String>

The result of wrapping the text to the provided indentation level and columns

Returns:

  • (Array<String>)

    the original line broken into several lines that adhere to the column cutoff and indentation level



40
41
42
43
44
# File 'lib/wrapomatic/line.rb', line 40

def wrapped
  @wrapped ||= indented.length <= columns ? 
    [indented] :
    Processor.process(self)
end