Class: Samovar::Output::Rows

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#levelObject (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

#columnsObject

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.

Returns:

  • (Boolean)


34
35
36
# File 'lib/samovar/output/rows.rb', line 34

def empty?
  @rows.empty?
end

#firstObject

Get the first row.



41
42
43
# File 'lib/samovar/output/rows.rb', line 41

def first
  @rows.first
end

#indentationObject

Get the indentation string for this level.



55
56
57
# File 'lib/samovar/output/rows.rb', line 55

def indentation
  @indentation ||= "\t" * @level
end

#lastObject

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.

Yields:

  • (nested_rows)


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