class Charty::TableAdapters::ActiveRecordAdapter
Attributes
column_names[R]
data[R]
Public Class Methods
new(data)
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 10 def initialize(data) @data = check_type(ActiveRecord::Relation, data, :data) @column_names = @data.column_names.freeze self.columns = Index.new(@column_names) self.index = RangeIndex.new(0 ... length) end
supported?(data)
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 6 def self.supported?(data) defined?(ActiveRecord::Relation) && data.is_a?(ActiveRecord::Relation) end
Public Instance Methods
[](row, column)
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 27 def [](row, column) fetch_records unless @columns_cache if row @columns_cache[resolve_column_index(column)][row] else column_data = @columns_cache[resolve_column_index(column)] Vector.new(column_data, index: index, name: column) end end
column_length()
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 23 def column_length column_names.length end
Private Instance Methods
check_type(type, data, name)
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 57 def check_type(type, data, name) return data if data.is_a?(type) raise TypeError, "#{name} must be a #{type}" end
fetch_records()
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 53 def fetch_records @columns_cache = @data.pluck(*column_names).transpose end
resolve_column_index(column)
click to toggle source
# File lib/charty/table_adapters/active_record_adapter.rb, line 37 def resolve_column_index(column) case column when String, Symbol index = column_names.index(column.to_s) unless index raise IndexError, "invalid column name: #{column.inspect}" end index when Integer column else message = "column must be String or Integer: #{column.inspect}" raise ArgumentError, message end end