Class: Table

Inherits:
Object
  • Object
show all
Defined in:
lib/base/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Table

Returns a new instance of Table.



3
4
5
6
7
8
9
# File 'lib/base/table.rb', line 3

def initialize(opts = {})
  @rows = []
  @columns = opts.fetch(:column_names)

  @make_index = opts.fetch(:make_index) {true}
  @metric_index = {}
end

Instance Method Details

#<<(row) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/base/table.rb', line 11

def <<(row)
  record = nil
  if row.is_a?(Record) || row.is_a?(CodeIssue)
    record = row
  else
    record = Record.new(row, @columns)
  end
  @rows << record
  updated_key_index(record) if @make_index
end

#[](index) ⇒ Object



36
37
38
# File 'lib/base/table.rb', line 36

def [](index)
  @rows[index]
end

#column(column_name) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/base/table.rb', line 40

def column(column_name)
  arr = []
  @rows.each do |row|
    arr << row[column_name]
  end
  arr
end

#delete_at(index) ⇒ Object



60
61
62
# File 'lib/base/table.rb', line 60

def delete_at(index)
  @rows.delete_at(index)
end

#eachObject



22
23
24
25
26
# File 'lib/base/table.rb', line 22

def each
  @rows.each do |row|
    yield row
  end
end

#group_by_metricObject



48
49
50
# File 'lib/base/table.rb', line 48

def group_by_metric
  @metric_index.to_a
end

#lengthObject



32
33
34
# File 'lib/base/table.rb', line 32

def length
  @rows.length
end

#mapObject



68
69
70
71
72
73
74
# File 'lib/base/table.rb', line 68

def map
  new_table = Table.new(:column_names => @columns)
  @rows.map do |row|
    new_table << (yield row)
  end
  new_table
end

#rows_with(conditions) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/base/table.rb', line 52

def rows_with(conditions)
  if optimized_conditions?(conditions)
    optimized_select(conditions)
  else
    slow_select(conditions)
  end
end

#sizeObject



28
29
30
# File 'lib/base/table.rb', line 28

def size
  length
end

#to_aObject



64
65
66
# File 'lib/base/table.rb', line 64

def to_a
  @rows
end