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. - #empty? ⇒ Boolean
-
#has_key?(key) ⇒ Boolean
Deprecated.
-
#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.
-
#key?(key) ⇒ Boolean
Is the a Column with this key? Keys are lower-case, underscore symbols.
-
#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.
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tabular/columns.rb', line 20 def initialize(table = Table.new, names = [], column_mapper = nil) @table = table @renderer = nil 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
16 17 18 |
# File 'lib/tabular/columns.rb', line 16 def column_mapper @column_mapper ||= Tabular::ColumnMapper.new end |
#renderer(key) ⇒ Object
Renderer for Column key
. Default to Table#Renderer.
104 105 106 |
# File 'lib/tabular/columns.rb', line 104 def renderer(key) renderers[key] || @renderer || Renderer end |
Instance Method Details
#<<(key) ⇒ Object
Add a new Column with key
78 79 80 81 82 83 84 85 86 |
# File 'lib/tabular/columns.rb', line 78 def <<(key) column = Column.new(@table, self, key) return if is_blank?(column.key) || key?(key) @column_indexes[column.key] = @columns.size @column_indexes[@columns.size] = column @columns_by_key[column.key] = column @columns << column end |
#[](key) ⇒ Object
Column for key
63 64 65 |
# File 'lib/tabular/columns.rb', line 63 def [](key) @columns_by_key[key_to_sym(key)] end |
#delete(key) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/tabular/columns.rb', line 88 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
73 74 75 |
# File 'lib/tabular/columns.rb', line 73 def each(&block) @columns.each(&block) end |
#empty? ⇒ Boolean
47 48 49 |
# File 'lib/tabular/columns.rb', line 47 def empty? size.zero? end |
#has_key?(key) ⇒ Boolean
Deprecated
52 53 54 |
# File 'lib/tabular/columns.rb', line 52 def has_key?(key) # rubocop:disable Naming/PredicateName key? key end |
#index(key) ⇒ Object
Zero-based index of Column for key
68 69 70 |
# File 'lib/tabular/columns.rb', line 68 def index(key) @column_indexes[key] end |
#key?(key) ⇒ Boolean
Is the a Column with this key? Keys are lower-case, underscore symbols. Example: :postal_code
58 59 60 |
# File 'lib/tabular/columns.rb', line 58 def key?(key) @columns.any? { |column| column.key == key } end |
#renderers ⇒ Object
List of Renderers
109 110 111 |
# File 'lib/tabular/columns.rb', line 109 def renderers @renderers ||= {} end |
#set_columns(table = Table.new, names = []) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tabular/columns.rb', line 31 def set_columns(table = Table.new, names = []) index = 0 names = names.keys if names.respond_to?(:keys) @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 += 1 new_column end end |
#size ⇒ Object
Count of Columns#columns
99 100 101 |
# File 'lib/tabular/columns.rb', line 99 def size @columns.size end |
#to_space_delimited ⇒ Object
113 114 115 |
# File 'lib/tabular/columns.rb', line 113 def to_space_delimited map(&:to_space_delimited).join " " end |