Class: Turntable::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/turntable/table.rb

Overview

Turntable::Table is Turntable’s main interface. It maintains an ordered list of Turntable::Row objects, according to a given Turntable::TableHeader.

Internally, Turntable::Table implements a SortedSet. See the documentation for Turntable::TableHeader for the reasons why. Additionally, the SortedSet ensures that two otherwise identical rows will have differing positions within the table.

Turntable::Table includes Enumerable, so every method there is available here. Also, calling Turntable::Table#rows returns the internal SortedSet, so you can gain access to the instance methods available for sets that way.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ Table

Associates a TableHeader with a new, empty Table.



34
35
36
37
# File 'lib/turntable/table.rb', line 34

def initialize(header)
  @rows = SortedSet.new
  @header = header
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



29
30
31
# File 'lib/turntable/table.rb', line 29

def header
  @header
end

#rowsObject

Returns the value of attribute rows.



30
31
32
# File 'lib/turntable/table.rb', line 30

def rows
  @rows
end

Instance Method Details

#[](position) ⇒ Object

Retreives a row by its position.



59
60
61
# File 'lib/turntable/table.rb', line 59

def [](position)
  @rows.detect { |row| row.position == position }
end

#eachObject



25
26
27
# File 'lib/turntable/table.rb', line 25

def each
  @rows.each { |row| yield row }
end

#push(*data) ⇒ Object

Append a new Row to the Table.

t = Turntable::Table.new(some_header)
t.push "LP4", "Ratatat", 2010


44
45
46
47
# File 'lib/turntable/table.rb', line 44

def push(*data)
  r = Row.new(@header, get_new_position, data)
  @rows << r
end