Class: Charty::TableAdapters::DatasetsAdapter

Inherits:
BaseAdapter
  • Object
show all
Includes:
Enumerable
Defined in:
lib/charty/table_adapters/datasets_adapter.rb

Instance Attribute Summary

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(dataset) ⇒ DatasetsAdapter

Returns a new instance of DatasetsAdapter.



13
14
15
16
17
18
19
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 13

def initialize(dataset)
  @table = dataset.to_table
  @records = []

  self.columns = self.column_names
  self.index = 0 ... length
end

Class Method Details

.supported?(data) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 8

def self.supported?(data)
  defined?(Datasets::Dataset) &&
    data.is_a?(Datasets::Dataset)
end

Instance Method Details

#[](row, column) ⇒ Object

Parameters:

  • row (Integer)

    Row index

  • column (Symbol, String, Integer)

    Column index



45
46
47
48
49
50
51
52
53
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 45

def [](row, column)
  if row
    record = @table.find_record(row)
    return nil if record.nil?
    record[column]
  else
    Vector.new(@table[column], index: index, name: column)
  end
end

#column_lengthObject



25
26
27
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 25

def column_length
  column_names.length
end

#column_namesObject



29
30
31
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 29

def column_names
  @table.column_names
end

#dataObject



21
22
23
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 21

def data
  @table
end

#each(&block) ⇒ Object



37
38
39
40
41
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 37

def each(&block)
  return to_enum(__method__) unless block_given?

  @table.each_record(&block)
end

#lengthObject



33
34
35
# File 'lib/charty/table_adapters/datasets_adapter.rb', line 33

def length
  data.n_rows
end