SamovarSourceSamovarOutputRows

class Rows

Represents a collection of rows for usage output.

Manages hierarchical usage information with support for nesting and formatting.

Definitions

def initialize(level = 0)

Initialize a new rows collection.

Signature

parameter level Integer

The indentation level for this collection.

Implementation

def initialize(level = 0)
	@level = level
	@rows = []
end

attr :level

The indentation level.

Signature

attribute Integer

def empty?

Check if this collection is empty.

Signature

returns Boolean

True if there are no rows.

Implementation

def empty?
	@rows.empty?
end

def first

Get the first row.

Signature

returns Object | Nil

The first row.

Implementation

def first
	@rows.first
end

def last

Get the last row.

Signature

returns Object | Nil

The last row.

Implementation

def last
	@rows.last
end

def indentation

Get the indentation string for this level.

Signature

returns String

The indentation string.

Implementation

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

def each(ignore_nested: false, &block)

Iterate over each row.

Signature

parameter ignore_nested Boolean

Whether to skip nested rows.

yields {|row, rows| ...}

Each row with its parent collection.

Implementation

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

def <<(object)

Add a row to this collection.

Signature

parameter object Object

The object to add as a row.

returns Rows

Self.

Implementation

def << object
	@rows << Row.new(object)
	
	return self
end

def columns

Get the columns for alignment.

Signature

returns Columns

The columns calculator.

Implementation

def columns
	@columns ||= Columns.new(@rows.select{|row| row.is_a? Array})
end

def nested(*arguments)

Create a nested section in the output.

Signature

parameter arguments Array

Arguments for the header.

yields {|rows| ...}

A block that populates the nested rows.

Implementation

def nested(*arguments)
	@rows << Header.new(*arguments)
	
	nested_rows = self.class.new(@level + 1)
	
	yield nested_rows
	
	@rows << nested_rows
end