Class: Wrapomatic::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/wrapomatic/wrapper.rb

Overview

Wrap several lines

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, indents = 0, columns = 80) ⇒ Wrapper

Returns a new instance of Wrapper.

Parameters:

  • text (String)

    the text to wrap

  • indents (Integer) (defaults to: 0)

    the level to which each line should be indented

  • columns (Integer) (defaults to: 80)

    the column to which each line should be wrapped



28
29
30
31
32
# File 'lib/wrapomatic/wrapper.rb', line 28

def initialize(text, indents = 0, columns = 80)
  @text, @indents, @columns = text, indents, columns
  @lines = []
  spit_some_mad_fire
end

Instance Attribute Details

#columnsInteger (readonly)

Returns the column cutoff.

Returns:

  • (Integer)

    the column cutoff



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
# File 'lib/wrapomatic/wrapper.rb', line 18

class Wrapper
  attr_reader :text, :lines, :indents, :columns

  # @param text [String] the text to wrap
  #
  # @param indents [Integer] the level to which each line should be
  #   indented
  #
  # @param columns [Integer] the column to which each line should be
  #   wrapped
  def initialize(text, indents = 0, columns = 80)
    @text, @indents, @columns = text, indents, columns
    @lines = []
    spit_some_mad_fire
  end

  # The text wrapped to the desired indentation level and column cutoff
  #
  # @return [String] the wrapped text, joined by newlines
  def wrapped
    @lines.join("\n")
  end

  private
  def spit_some_mad_fire
    @lines = unwrapped_lines.map {|line|
      Line.new(line, indents, columns).wrapped
    }.flatten
  end

  def unwrapped_lines
    text.split("\n")
  end
end

#indentsInteger (readonly)

Returns the indentation level.

Returns:

  • (Integer)

    the indentation level



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
# File 'lib/wrapomatic/wrapper.rb', line 18

class Wrapper
  attr_reader :text, :lines, :indents, :columns

  # @param text [String] the text to wrap
  #
  # @param indents [Integer] the level to which each line should be
  #   indented
  #
  # @param columns [Integer] the column to which each line should be
  #   wrapped
  def initialize(text, indents = 0, columns = 80)
    @text, @indents, @columns = text, indents, columns
    @lines = []
    spit_some_mad_fire
  end

  # The text wrapped to the desired indentation level and column cutoff
  #
  # @return [String] the wrapped text, joined by newlines
  def wrapped
    @lines.join("\n")
  end

  private
  def spit_some_mad_fire
    @lines = unwrapped_lines.map {|line|
      Line.new(line, indents, columns).wrapped
    }.flatten
  end

  def unwrapped_lines
    text.split("\n")
  end
end

#linesArray<String> (readonly)

Returns the text after wrapping as individual lines.

Returns:

  • (Array<String>)

    the text after wrapping as individual lines



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
# File 'lib/wrapomatic/wrapper.rb', line 18

class Wrapper
  attr_reader :text, :lines, :indents, :columns

  # @param text [String] the text to wrap
  #
  # @param indents [Integer] the level to which each line should be
  #   indented
  #
  # @param columns [Integer] the column to which each line should be
  #   wrapped
  def initialize(text, indents = 0, columns = 80)
    @text, @indents, @columns = text, indents, columns
    @lines = []
    spit_some_mad_fire
  end

  # The text wrapped to the desired indentation level and column cutoff
  #
  # @return [String] the wrapped text, joined by newlines
  def wrapped
    @lines.join("\n")
  end

  private
  def spit_some_mad_fire
    @lines = unwrapped_lines.map {|line|
      Line.new(line, indents, columns).wrapped
    }.flatten
  end

  def unwrapped_lines
    text.split("\n")
  end
end

#textString (readonly)

Returns the text to wrap.

Returns:

  • (String)

    the text to wrap



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
# File 'lib/wrapomatic/wrapper.rb', line 18

class Wrapper
  attr_reader :text, :lines, :indents, :columns

  # @param text [String] the text to wrap
  #
  # @param indents [Integer] the level to which each line should be
  #   indented
  #
  # @param columns [Integer] the column to which each line should be
  #   wrapped
  def initialize(text, indents = 0, columns = 80)
    @text, @indents, @columns = text, indents, columns
    @lines = []
    spit_some_mad_fire
  end

  # The text wrapped to the desired indentation level and column cutoff
  #
  # @return [String] the wrapped text, joined by newlines
  def wrapped
    @lines.join("\n")
  end

  private
  def spit_some_mad_fire
    @lines = unwrapped_lines.map {|line|
      Line.new(line, indents, columns).wrapped
    }.flatten
  end

  def unwrapped_lines
    text.split("\n")
  end
end

Instance Method Details

#wrappedString

The text wrapped to the desired indentation level and column cutoff

Returns:

  • (String)

    the wrapped text, joined by newlines



37
38
39
# File 'lib/wrapomatic/wrapper.rb', line 37

def wrapped
  @lines.join("\n")
end