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 = Table.new, names = [], column_mapper = nil) ⇒ Columns

Returns a new instance of Columns.



20
21
22
23
24
25
26
27
28
# File 'lib/tabular/columns.rb', line 20

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_mapperObject

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.



103
104
105
# File 'lib/tabular/columns.rb', line 103

def renderer(key)
  renderers[key] || @renderer || Renderer
end

Instance Method Details

#<<(key) ⇒ Object

Add a new Column with key



77
78
79
80
81
82
83
84
85
# File 'lib/tabular/columns.rb', line 77

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



62
63
64
# File 'lib/tabular/columns.rb', line 62

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

#delete(key) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/tabular/columns.rb', line 87

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



72
73
74
# File 'lib/tabular/columns.rb', line 72

def each(&block)
  @columns.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/tabular/columns.rb', line 46

def empty?
  size.zero?
end

#has_key?(key) ⇒ Boolean

Deprecated

Returns:

  • (Boolean)


51
52
53
# File 'lib/tabular/columns.rb', line 51

def has_key?(key) # rubocop:disable Naming/PredicateName
  key? key
end

#index(key) ⇒ Object

Zero-based index of Column for key



67
68
69
# File 'lib/tabular/columns.rb', line 67

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

Returns:

  • (Boolean)


57
58
59
# File 'lib/tabular/columns.rb', line 57

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

#renderersObject

List of Renderers



108
109
110
# File 'lib/tabular/columns.rb', line 108

def renderers
  @renderers ||= {}
end

#set_columns(table = Table.new, names = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tabular/columns.rb', line 30

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

#sizeObject

Count of Columns#columns



98
99
100
# File 'lib/tabular/columns.rb', line 98

def size
  @columns.size
end

#to_space_delimitedObject



112
113
114
# File 'lib/tabular/columns.rb', line 112

def to_space_delimited
  map(&:to_space_delimited).join "   "
end