Class: TableData::Column
- Inherits:
-
Object
- Object
- TableData::Column
- Includes:
- Enumerable
- Defined in:
- lib/tabledata/column.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #accessor ⇒ Object
- #each ⇒ Object
- #header ⇒ Object
-
#initialize(table, index) ⇒ Column
constructor
A new instance of Column.
- #to_a(include_header = true) ⇒ Object
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
#index ⇒ Object (readonly)
Returns the value of attribute index.
6 7 8 |
# File 'lib/tabledata/column.rb', line 6 def index @index end |
#table ⇒ Object (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 |
#accessor ⇒ Object
19 20 21 |
# File 'lib/tabledata/column.rb', line 19 def accessor @table.column_accessor(@index) end |
#each ⇒ Object
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 |
#header ⇒ Object
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 |