Class: Tabledata::Column
- Inherits:
-
Object
- Object
- Tabledata::Column
- Includes:
- Enumerable
- Defined in:
- lib/tabledata/column.rb
Overview
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
-
#index ⇒ Integer
readonly
The column index in the table, zero based.
-
#table ⇒ Tabledata::Table
readonly
The table this column belongs to.
Instance Method Summary collapse
-
#==(other) ⇒ 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. -
#[](*args) ⇒ Array, Object
Similar to Tabledata::Table#[], but only returns values for this column.
-
#accessor ⇒ Symbol?
The accessor for this column.
-
#each {|value| ... } ⇒ self
Iterate over all body values (i.e. no header or footer) in the column.
-
#each_value {|value| ... } ⇒ self
Iterate over all values in the column, including header and footer.
-
#eql?(other) ⇒ true, false
Whether
other
is also a column and contains the same data. -
#hash ⇒ Object
See Object#hash.
-
#header ⇒ Object
The header value of this column (if available).
-
#initialize(table, index) ⇒ Column
constructor
A new instance of Column.
-
#size ⇒ Integer
(also: #length)
The number of values, excluding headers and footer.
-
#to_a(options = nil) ⇒ Array
All values in the column, including header and footer.
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
#index ⇒ Integer (readonly)
17 18 19 |
# File 'lib/tabledata/column.rb', line 17 def index @index end |
#table ⇒ Tabledata::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 |
#accessor ⇒ Symbol?
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.
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.
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 |
#hash ⇒ Object
See Object#hash
147 148 149 |
# File 'lib/tabledata/column.rb', line 147 def hash [Tabledata::Column, to_a].hash end |
#header ⇒ Object
32 33 34 |
# File 'lib/tabledata/column.rb', line 32 def header @table.column_header(@index) end |
#size ⇒ Integer 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.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tabledata/column.rb', line 108 def to_a(=nil) data = @table.data.transpose[@index] if start_offset = [:include_header] && @table.headers? ? 1 : 0 end_offset = [:include_footer] && @table. ? -2 : -1 data[start_offset..end_offset] else data end end |