Class: Tabular::Columns
- Inherits:
-
Object
- Object
- Tabular::Columns
- Defined in:
- lib/tabular/columns.rb
Overview
The Table’s header: a list of Columns.
Instance Attribute Summary collapse
-
#column_mapper ⇒ Object
table
– Tabledata
– array of header names. -
#renderer(key) ⇒ Object
Renderer for Column
key
.
Instance Method Summary collapse
-
#<<(key) ⇒ Object
Add a new Column with
key
. -
#[](key) ⇒ Object
Column for
key
. - #delete(key) ⇒ Object
-
#each(&block) ⇒ Object
Call
block
for each Column. -
#has_key?(key) ⇒ Boolean
Is the a Column with this key? Keys are lower-case, underscore symbols.
-
#index(key) ⇒ Object
Zero-based index of Column for
key
. -
#initialize(table = Table.new, names = [], column_mapper = nil) ⇒ Columns
constructor
A new instance of Columns.
-
#renderers ⇒ Object
List of Renderers.
- #set_columns(table = Table.new, names = []) ⇒ Object
-
#size ⇒ Object
Count of Columns#columns.
- #to_space_delimited ⇒ Object
Methods included from Keys
Methods included from Blank
Constructor Details
#initialize(table = Table.new, names = [], column_mapper = nil) ⇒ Columns
Returns a new instance of Columns.
18 19 20 21 22 23 24 25 26 |
# File 'lib/tabular/columns.rb', line 18 def initialize(table = Table.new, names = [], column_mapper = nil) @table = table self.column_mapper = column_mapper @column_indexes = {} @columns_by_key = {} set_columns table, names end |
Instance Attribute Details
#column_mapper ⇒ Object
table
– Table data
– array of header names
14 15 16 |
# File 'lib/tabular/columns.rb', line 14 def column_mapper @column_mapper end |
#renderer(key) ⇒ Object
Renderer for Column key
. Default to Table#renderer.
94 95 96 |
# File 'lib/tabular/columns.rb', line 94 def renderer @renderer end |
Instance Method Details
#<<(key) ⇒ Object
Add a new Column with key
68 69 70 71 72 73 74 75 76 |
# File 'lib/tabular/columns.rb', line 68 def <<(key) column = Column.new(@table, self, key) unless is_blank?(column.key) || has_key?(key) @column_indexes[column.key] = @columns.size @column_indexes[@columns.size] = column @columns_by_key[column.key] = column @columns << column end end |
#[](key) ⇒ Object
Column for key
53 54 55 |
# File 'lib/tabular/columns.rb', line 53 def [](key) @columns_by_key[key_to_sym(key)] end |
#delete(key) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/tabular/columns.rb', line 78 def delete(key) @columns.delete_if { |column| column.key == key } @columns_by_key.delete key @column_indexes.delete key @columns.each.with_index do |column, index| @column_indexes[column.key] = index end end |
#each(&block) ⇒ Object
Call block
for each Column
63 64 65 |
# File 'lib/tabular/columns.rb', line 63 def each(&block) @columns.each(&block) end |
#has_key?(key) ⇒ Boolean
Is the a Column with this key? Keys are lower-case, underscore symbols. Example: :postal_code
48 49 50 |
# File 'lib/tabular/columns.rb', line 48 def has_key?(key) @columns.any? { |column| column.key == key } end |
#index(key) ⇒ Object
Zero-based index of Column for key
58 59 60 |
# File 'lib/tabular/columns.rb', line 58 def index(key) @column_indexes[key] end |
#renderers ⇒ Object
List of Renderers
99 100 101 |
# File 'lib/tabular/columns.rb', line 99 def renderers @renderers ||= {} end |
#set_columns(table = Table.new, names = []) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/tabular/columns.rb', line 28 def set_columns(table = Table.new, names = []) index = 0 if names.respond_to?(:keys) names = names.keys end @columns = names.map do |name| new_column = Tabular::Column.new(table, self, name) unless is_blank?(new_column.key) @column_indexes[new_column.key] = index @columns_by_key[new_column.key] = new_column end index = index + 1 new_column end end |
#size ⇒ Object
Count of Columns#columns
89 90 91 |
# File 'lib/tabular/columns.rb', line 89 def size @columns.size end |
#to_space_delimited ⇒ Object
103 104 105 |
# File 'lib/tabular/columns.rb', line 103 def to_space_delimited map(&:to_space_delimited).join " " end |