Class: TableData::Column

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tabledata/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, index) ⇒ Column

Returns a new instance of Column.



10
11
12
13
# File 'lib/tabledata/column.rb', line 10

def initialize(table, index)
  @table  = table
  @index  = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/tabledata/column.rb', line 6

def index
  @index
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/tabledata/column.rb', line 5

def table
  @table
end

Instance Method Details

#[](*args) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/tabledata/column.rb', line 23

def [](*args)
  rows = @table.rows[*args]

  if rows.is_a?(Array) # slice
    rows.map { |row| row.at(@index) }
  else # single row
    rows.at(@index)
  end
end

#accessorObject



19
20
21
# File 'lib/tabledata/column.rb', line 19

def accessor
  @table.column_accessor(@index)
end

#eachObject



33
34
35
36
37
38
39
# File 'lib/tabledata/column.rb', line 33

def each
  return enum_for(__method__) unless block_given?

  @table.each do |row|
    yield row.at(@index)
  end
end

#headerObject



15
16
17
# File 'lib/tabledata/column.rb', line 15

def header
  @table.column_header(@index)
end

#to_a(include_header = true) ⇒ Object



41
42
43
44
45
# File 'lib/tabledata/column.rb', line 41

def to_a(include_header=true)
  data = @table.data.transpose[@index]

  include_header || !@table.headers? ? data : data[1..-1]
end