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.



9
10
11
12
13
14
15
# File 'lib/base/table.rb', line 9

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

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

Instance Method Details

#<<(row) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/base/table.rb', line 17

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

#[](index) ⇒ Object



42
43
44
# File 'lib/base/table.rb', line 42

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

#column(column_name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/base/table.rb', line 46

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

#delete_at(index) ⇒ Object



66
67
68
# File 'lib/base/table.rb', line 66

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

#eachObject



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

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

#group_by_metricObject



54
55
56
# File 'lib/base/table.rb', line 54

def group_by_metric
  @metric_index.to_a
end

#lengthObject



38
39
40
# File 'lib/base/table.rb', line 38

def length
  @rows.length
end

#mapObject



74
75
76
77
78
79
80
# File 'lib/base/table.rb', line 74

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



58
59
60
61
62
63
64
# File 'lib/base/table.rb', line 58

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

#sizeObject



34
35
36
# File 'lib/base/table.rb', line 34

def size
  length
end

#to_aObject



70
71
72
# File 'lib/base/table.rb', line 70

def to_a
  @rows
end