Class: Charty::TableAdapters::ActiveRecordAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/charty/table_adapters/active_record_adapter.rb

Instance Attribute Summary collapse

Attributes inherited from BaseAdapter

#columns, #index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

#==, #column?, #compare_data_equality, #drop_na, #group_by, #melt, #sort_values

Constructor Details

#initialize(data) ⇒ ActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.



10
11
12
13
14
15
# 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

Instance Attribute Details

#column_namesObject (readonly)

Returns the value of attribute column_names.



17
18
19
# File 'lib/charty/table_adapters/active_record_adapter.rb', line 17

def column_names
  @column_names
end

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/charty/table_adapters/active_record_adapter.rb', line 17

def data
  @data
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/charty/table_adapters/active_record_adapter.rb', line 6

def self.supported?(data)
  defined?(ActiveRecord::Relation) && data.is_a?(ActiveRecord::Relation)
end

Instance Method Details

#[](row, column) ⇒ Object



27
28
29
30
31
32
33
34
35
# 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_lengthObject



23
24
25
# File 'lib/charty/table_adapters/active_record_adapter.rb', line 23

def column_length
  column_names.length
end