Class: Tabular::Columns

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Blank, Keys
Defined in:
lib/tabular/columns.rb

Overview

The Table’s header: a list of Columns.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Keys

#key_to_sym

Methods included from Blank

#is_blank?

Constructor Details

#initialize(table, names, columns_map = {}) ⇒ Columns

table – Table data – array of header names columns_map – see Table. Maps column names and type conversion.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tabular/columns.rb', line 13

def initialize(table, names, columns_map = {})
  @table = table
  columns_map ||= {}
  @columns_map = normalize_columns_map(columns_map)
  @column_indexes = {}
  @columns_by_key = {}
  index = 0
  @columns = nil
  @columns = names.map do |column|
    new_column = Tabular::Column.new(table, self, column, @columns_map)
    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

Instance Attribute Details

#renderer(key) ⇒ Object

Renderer for Column key. Default to Table#renderer.



80
81
82
# File 'lib/tabular/columns.rb', line 80

def renderer
  @renderer
end

Instance Method Details

#<<(key) ⇒ Object

Add a new Column with key



54
55
56
57
58
59
60
61
62
# File 'lib/tabular/columns.rb', line 54

def <<(key)
  column = Column.new(@table, self, key, @columns_map)
  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



39
40
41
# File 'lib/tabular/columns.rb', line 39

def [](key)
  @columns_by_key[key_to_sym(key)]
end

#delete(key) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/tabular/columns.rb', line 64

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



49
50
51
# File 'lib/tabular/columns.rb', line 49

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

Returns:

  • (Boolean)


34
35
36
# File 'lib/tabular/columns.rb', line 34

def has_key?(key)
  @columns.any? { |column| column.key == key }
end

#index(key) ⇒ Object

Zero-based index of Column for key



44
45
46
# File 'lib/tabular/columns.rb', line 44

def index(key)
  @column_indexes[key]
end

#renderersObject

List of Renderers



85
86
87
# File 'lib/tabular/columns.rb', line 85

def renderers
  @renderers ||= {}
end

#sizeObject

Count of Columns#columns



75
76
77
# File 'lib/tabular/columns.rb', line 75

def size
  @columns.size
end