Class: Wrapomatic::Line
- Inherits:
-
Object
- Object
- Wrapomatic::Line
- 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
-
#columns ⇒ Integer
readonly
The columns cutoff.
-
#indents ⇒ Integer
readonly
The indentation level.
-
#original ⇒ String
readonly
The original text.
Instance Method Summary collapse
-
#indented ⇒ String
The indented line before wrapping.
-
#initialize(original, indents, columns) ⇒ Line
constructor
A new instance of Line.
-
#wrapped ⇒ Array<String>
The result of wrapping the text to the provided indentation level and columns.
Constructor Details
#initialize(original, indents, columns) ⇒ Line
Returns a new instance of Line.
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
#columns ⇒ Integer (readonly)
Returns 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 |
#indents ⇒ Integer (readonly)
Returns 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 |
#original ⇒ String (readonly)
Returns 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
#indented ⇒ String
The indented line before wrapping
49 50 51 |
# File 'lib/wrapomatic/line.rb', line 49 def indented "#{INDENTATION * indents}#{original}" end |