Class: Tabledata::Column

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

Overview

Note:

The column data is not copied. It is retrieved from the table via the columns index. If you mutate the table, adding a column to it, this column instance's data will change.

Represents a column in a table and provides an easy way to enumerate values in a column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, index) ⇒ Column


25
26
27
28
# File 'lib/tabledata/column.rb', line 25

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

Instance Attribute Details

#indexInteger (readonly)


17
18
19
# File 'lib/tabledata/column.rb', line 17

def index
  @index
end

#tableTabledata::Table (readonly)


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

def table
  @table
end

Instance Method Details

#==(other) ⇒ true, false


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tabledata/column.rb', line 127

def ==(other)
  if other.is_a?(Tabledata::Column)
    other.to_a == @table.data.transpose[@index]
  elsif other.is_a?(Array)
    other == @table.data.transpose[@index]
  elsif other.respond_to?(:to_ary)
    other.to_ary == @table.data.transpose[@index]
  else
    false
  end
end

#[](*args) ⇒ Array, Object

Similar to Tabledata::Table#[], but only returns values for this column. Provides array like access to this column's data. Only considers body values (i.e., does not consider header and footer).


47
48
49
50
51
52
53
54
55
# File 'lib/tabledata/column.rb', line 47

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

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

#accessorSymbol?


38
39
40
# File 'lib/tabledata/column.rb', line 38

def accessor
  @table.column_accessor(@index)
end

#each {|value| ... } ⇒ self

Iterate over all body values (i.e. no header or footer) in the column.

Yields:

  • (value)

Yield Parameters:

  • (Object)

See Also:


66
67
68
69
70
71
72
73
74
# File 'lib/tabledata/column.rb', line 66

def each
  return enum_for(__method__) unless block_given?

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

  self
end

#each_value {|value| ... } ⇒ self

Iterate over all values in the column, including header and footer.

Yields:

  • (value)

Yield Parameters:

  • (Object)

See Also:


85
86
87
88
89
90
91
92
93
# File 'lib/tabledata/column.rb', line 85

def each_value
  return enum_for(__method__) unless block_given?

  @table.data.each do |row|
    yield(row[@index])
  end

  self
end

#eql?(other) ⇒ true, false


142
143
144
# File 'lib/tabledata/column.rb', line 142

def eql?(other)
  other.is_a?(Tabledata::Column) && @table.data.transpose[@index] == other.to_a
end

#hashObject

See Object#hash


147
148
149
# File 'lib/tabledata/column.rb', line 147

def hash
  [Tabledata::Column, to_a].hash
end

#headerObject


32
33
34
# File 'lib/tabledata/column.rb', line 32

def header
  @table.column_header(@index)
end

#sizeInteger Also known as: length


96
97
98
# File 'lib/tabledata/column.rb', line 96

def size
  @table.size
end

#to_a(options = nil) ⇒ Array

Returns All values in the column, including header and footer.

Options Hash (options):

  • :include_header (Symbol)

    Defaults to true. If set to false, the header (if present) is excluded.

  • :include_footer (Symbol)

    Defaults to true. If set to false, the footer (if present) is excluded.


108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tabledata/column.rb', line 108

def to_a(options=nil)
  data = @table.data.transpose[@index]

  if options
    start_offset = options[:include_header] && @table.headers? ? 1 : 0
    end_offset   = options[:include_footer] && @table.footer? ? -2 : -1

    data[start_offset..end_offset]
  else
    data
  end
end