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)



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

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.



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

def index
  @index
end

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/tabular/row.rb', line 13

def source
  @source
end

Instance Method Details

#[](key) ⇒ Object

Cell value by symbol. E.g., row



37
38
39
# File 'lib/tabular/row.rb', line 37

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_



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

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

#blank?Boolean

Returns:

  • (Boolean)


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

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

#columnsObject

Tabluar::Columns



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

def columns
  @table.columns
end

#delete(key) ⇒ Object



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

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

#each(&block) ⇒ Object

Call block for each cell



53
54
55
# File 'lib/tabular/row.rb', line 53

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

#each_key(&block) ⇒ Object

Call block for each key



58
59
60
# File 'lib/tabular/row.rb', line 58

def each_key(&block)
  keys.each(&block)
end

#each_with_key(&block) ⇒ Object

Call block for each cell



63
64
65
# File 'lib/tabular/row.rb', line 63

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

#inspectObject



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

def inspect
  hash.inspect
end

#join(sep = nil) ⇒ Object

For pretty-printing cell values



73
74
75
# File 'lib/tabular/row.rb', line 73

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

#keysObject

Keys for all columns



68
69
70
# File 'lib/tabular/row.rb', line 68

def keys
  hash.keys
end

#last?Boolean

Is this the last row?

Returns:

  • (Boolean)


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

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

#metadataObject



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

def 
  @table.
end

#nextObject

Next Row



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

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

#previousObject

Previous Row



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

def previous
  @table.rows[index - 1] if index.positive?
end

#render(key) ⇒ Object

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



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

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

#to_hashObject



116
117
118
# File 'lib/tabular/row.rb', line 116

def to_hash
  hash.dup
end

#to_sObject



134
135
136
# File 'lib/tabular/row.rb', line 134

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

#to_space_delimitedObject



120
121
122
123
124
125
126
127
128
# File 'lib/tabular/row.rb', line 120

def to_space_delimited
  cells = []

  hash.each_key do |key|
    cells << (render(key) || "").ljust(columns[key].width)
  end

  cells.join "   "
end