Class: Tabular::Row

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

Overview

Associate list of cells. Each Table has a list of Rows. Access Row cells via symbols. Ex: row

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, cells = [], source = nil) ⇒ Row

table – Table cells – array (not neccessarily Strings) source – original data before mapped to Hash or Array (optional)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tabular/row.rb', line 16

def initialize(table, cells = [], source = nil)
  @table = table
  @source = source || cells

  if cells.respond_to?(:keys)
    @array = cells.values
    @hash = {}
    cells.each do |key, value|
      @hash[key_to_sym(key)] = value
    end
  else
    @array = cells
    @hash = nil
  end

  @index = table.rows.size
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



10
11
12
# File 'lib/tabular/row.rb', line 10

def index
  @index
end

#sourceObject (readonly)

Returns the value of attribute source.



11
12
13
# File 'lib/tabular/row.rb', line 11

def source
  @source
end

Instance Method Details

#[](key) ⇒ Object

Cell value by symbol. E.g., row



35
36
37
# File 'lib/tabular/row.rb', line 35

def [](key)
  hash[key]
end

#[]=(key, value) ⇒ Object

Set cell value. Adds cell to end of Row and adds new Column if there is no Column for +key_



40
41
42
43
44
45
46
47
48
# File 'lib/tabular/row.rb', line 40

def []=(key, value)
  if columns.has_key?(key)
    @array[columns.index(key)] = value
  else
    @array << value
    columns << key
  end
  hash[key] = value
end

#blank?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/tabular/row.rb', line 92

def blank?
  @array.all? { |cell| is_blank?(cell) }
end

#columnsObject

Tabluar::Columns



97
98
99
# File 'lib/tabular/row.rb', line 97

def columns
  @table.columns
end

#delete(key) ⇒ Object



70
71
72
73
# File 'lib/tabular/row.rb', line 70

def delete(key)
  @array.delete key
  hash.delete key
end

#each(&block) ⇒ Object

Call block for each cell



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

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

#each_with_key(&block) ⇒ Object

Call block for each cell



56
57
58
# File 'lib/tabular/row.rb', line 56

def each_with_key(&block)
  hash.each(&block)
end

#inspectObject



125
126
127
# File 'lib/tabular/row.rb', line 125

def inspect
  hash.inspect
end

#join(sep = nil) ⇒ Object

For pretty-printing cell values



66
67
68
# File 'lib/tabular/row.rb', line 66

def join(sep = nil)
  @array.join(sep)
end

#keysObject

Keys for all columns



61
62
63
# File 'lib/tabular/row.rb', line 61

def keys
  hash.keys
end

#last?Boolean

Is this the last row?

Returns:

  • (Boolean)


88
89
90
# File 'lib/tabular/row.rb', line 88

def last?
  index == @table.rows.size - 1
end

#metadataObject



107
108
109
# File 'lib/tabular/row.rb', line 107

def 
  @table.
end

#nextObject

Next Row



83
84
85
# File 'lib/tabular/row.rb', line 83

def next
  @table.rows[index + 1]
end

#previousObject

Previous Row



76
77
78
79
80
# File 'lib/tabular/row.rb', line 76

def previous
  if index > 0
    @table.rows[index - 1]
  end
end

#render(key) ⇒ Object

By default, return self. Customize by setting Table#renderer or Column#renderers



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

def render(key)
  column = columns[key]
  column.renderer.render column, self
end

#to_hashObject



111
112
113
# File 'lib/tabular/row.rb', line 111

def to_hash
  hash.dup
end

#to_sObject



129
130
131
# File 'lib/tabular/row.rb', line 129

def to_s
  @array.join(", ").to_s
end

#to_space_delimitedObject



115
116
117
118
119
120
121
122
123
# File 'lib/tabular/row.rb', line 115

def to_space_delimited
  _cells = []

  hash.each do |key, _|
    _cells << (render(key) || "").ljust(columns[key].width)
  end

  _cells.join "   "
end