class Table
Public Class Methods
new(opts = {})
click to toggle source
# 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
Public Instance Methods
<<(row)
click to toggle source
# 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)
click to toggle source
# File lib/base/table.rb, line 42 def [](index) @rows[index] end
column(column_name)
click to toggle source
# 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)
click to toggle source
# File lib/base/table.rb, line 66 def delete_at(index) @rows.delete_at(index) end
each() { |row| ... }
click to toggle source
# File lib/base/table.rb, line 28 def each @rows.each do |row| yield row end end
group_by_metric()
click to toggle source
# File lib/base/table.rb, line 54 def group_by_metric @metric_index.to_a end
length()
click to toggle source
# File lib/base/table.rb, line 38 def length @rows.length end
map() { |row)| ... }
click to toggle source
# 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)
click to toggle source
# File lib/base/table.rb, line 58 def rows_with(conditions) if optimized_conditions?(conditions) optimized_select(conditions) else slow_select(conditions) end end
size()
click to toggle source
# File lib/base/table.rb, line 34 def size length end
to_a()
click to toggle source
# File lib/base/table.rb, line 70 def to_a @rows end
Private Instance Methods
optimized_conditions?(conditions)
click to toggle source
# File lib/base/table.rb, line 84 def optimized_conditions?(conditions) conditions.keys.length == 1 && conditions.keys.first.to_sym == :metric end
optimized_select(conditions)
click to toggle source
# File lib/base/table.rb, line 88 def optimized_select(conditions) metric = (conditions['metric'] || conditions[:metric]).to_s @metric_index[metric].to_a.clone end
slow_select(conditions)
click to toggle source
# File lib/base/table.rb, line 93 def slow_select(conditions) @rows.select do |row| conditions.all? do |key, value| row.has_key?(key.to_s) && row[key.to_s] == value end end end
updated_key_index(record)
click to toggle source
# File lib/base/table.rb, line 101 def updated_key_index(record) if record.has_key?('metric') @metric_index[record.metric] ||= Table.new(:column_names => @columns, :make_index => false) @metric_index[record.metric] << record end end