Class: Samovar::Output::Rows
- Inherits:
-
Object
- Object
- Samovar::Output::Rows
- Includes:
- Enumerable
- Defined in:
- lib/samovar/output/rows.rb
Overview
Represents a collection of rows for usage output.
Manages hierarchical usage information with support for nesting and formatting.
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
The indentation level.
Instance Method Summary collapse
-
#<<(object) ⇒ Object
Add a row to this collection.
-
#columns ⇒ Object
Get the columns for alignment.
-
#each(ignore_nested: false, &block) ⇒ Object
Iterate over each row.
-
#empty? ⇒ Boolean
Check if this collection is empty.
-
#first ⇒ Object
Get the first row.
-
#indentation ⇒ Object
Get the indentation string for this level.
-
#initialize(level = 0) ⇒ Rows
constructor
Initialize a new rows collection.
-
#last ⇒ Object
Get the last row.
-
#nested(*arguments) {|nested_rows| ... } ⇒ Object
Create a nested section in the output.
Constructor Details
#initialize(level = 0) ⇒ Rows
Initialize a new rows collection.
21 22 23 24 |
# File 'lib/samovar/output/rows.rb', line 21 def initialize(level = 0) @level = level @rows = [] end |
Instance Attribute Details
#level ⇒ Object (readonly)
The indentation level.
29 30 31 |
# File 'lib/samovar/output/rows.rb', line 29 def level @level end |
Instance Method Details
#<<(object) ⇒ Object
Add a row to this collection.
79 80 81 82 83 |
# File 'lib/samovar/output/rows.rb', line 79 def << object @rows << Row.new(object) return self end |
#columns ⇒ Object
Get the columns for alignment.
88 89 90 |
# File 'lib/samovar/output/rows.rb', line 88 def columns @columns ||= Columns.new(@rows.select{|row| row.is_a? Array}) end |
#each(ignore_nested: false, &block) ⇒ Object
Iterate over each row.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/samovar/output/rows.rb', line 63 def each(ignore_nested: false, &block) return to_enum(:each, ignore_nested: ignore_nested) unless block_given? @rows.each do |row| if row.is_a?(self.class) row.each(&block) unless ignore_nested else yield row, self end end end |
#empty? ⇒ Boolean
Check if this collection is empty.
34 35 36 |
# File 'lib/samovar/output/rows.rb', line 34 def empty? @rows.empty? end |
#first ⇒ Object
Get the first row.
41 42 43 |
# File 'lib/samovar/output/rows.rb', line 41 def first @rows.first end |
#indentation ⇒ Object
Get the indentation string for this level.
55 56 57 |
# File 'lib/samovar/output/rows.rb', line 55 def indentation @indentation ||= "\t" * @level end |
#last ⇒ Object
Get the last row.
48 49 50 |
# File 'lib/samovar/output/rows.rb', line 48 def last @rows.last end |
#nested(*arguments) {|nested_rows| ... } ⇒ Object
Create a nested section in the output.
96 97 98 99 100 101 102 103 104 |
# File 'lib/samovar/output/rows.rb', line 96 def nested(*arguments) @rows << Header.new(*arguments) nested_rows = self.class.new(@level + 1) yield nested_rows @rows << nested_rows end |