Class: Turntable::TableHeader

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

Overview

A Turntable::TableHeader maintains an ordered list of ColumnDefinition objects. This list is implemented as a SortedSet for three reasons:

* It's apparently faster than Array.
* The columns appear in order in IRB.
* It ensures there are no duplicate column definitions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*column_names) ⇒ TableHeader

Creates a new TableHeader. The given column names should be symbols.

Turntable::TableHeader.new(:album, :artist, :year)


31
32
33
34
35
36
# File 'lib/turntable/tableheader.rb', line 31

def initialize(*column_names)
  @columns = SortedSet.new
  column_names.each_with_index do |column, i|
    @columns << Turntable::ColumnDefinition.new(column, i)
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

Instance Method Details

#[](position) ⇒ Object

Returns a ColumnDefinition by specifying its position within the TableHeader.

th = Turntable::TableHeader.new(:album, :artist, :year)
th[1]   # => #<Turntable::ColumnDefinition @name=... @position=...>


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

def [](position)
  @columns.detect { |column| column.position == position }
end

#eachObject



21
22
23
# File 'lib/turntable/tableheader.rb', line 21

def each
  @columns.each { |column| yield column }
end