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

Returns a new instance of Column.

Parameters:

  • table (Tabledata::Table)

    The table this column belongs to.

  • index (Integer)

    The index of the column in the table.


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)

Returns The column index in the table, zero based.

Returns:

  • (Integer)

    The column index in the table, zero based.


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

def index
  @index
end

#tableTabledata::Table (readonly)

Returns The table this column belongs to.

Returns:


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

def table
  @table
end

Instance Method Details

#==(other) ⇒ true, false

Returns Whether the values in this column is equal to the values in other
False if other is neither a Tabledata::Column, Array, or Object responding to #to_ary.

Parameters:

Returns:

  • (true, false)

    Whether the values in this column is equal to the values in other
    False if other is neither a Tabledata::Column, Array, or Object responding to #to_ary.


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).

Returns:

  • (Array, Object)

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?

Returns The accessor for this column.

Returns:

  • (Symbol, nil)

    The accessor for this column


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)

Returns:

  • (self)

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)

Returns:

  • (self)

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

Returns Whether other is also a column and contains the same data.

Parameters:

  • (Object)

Returns:

  • (true, false)

    Whether other is also a column and contains the same data.


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

Returns The header value of this column (if available).

Returns:

  • (Object)

    The header value of this column (if available)


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

def header
  @table.column_header(@index)
end

#sizeInteger Also known as: length

Returns The number of values, excluding headers and footer.

Returns:

  • (Integer)

    The number of values, excluding headers and footer


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.

Parameters:

  • options (Hash) (defaults to: nil)

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.

Returns:

  • (Array)

    All values in the column, including header and footer.


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