class Charty::Table

Attributes

adapter[R]

Public Class Methods

new(data, **kwargs) click to toggle source
# File lib/charty/table.rb, line 17
def initialize(data, **kwargs)
  adapter_class = TableAdapters.find_adapter_class(data)
  if kwargs.empty?
    @adapter = adapter_class.new(data)
  else
    @adapter = adapter_class.new(data, **kwargs)
  end

  @column_cache = {}
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/charty/table.rb, line 39
def ==(other)
  return true if equal?(other)

  case other
  when Charty::Table
    adapter == other.adapter
  else
    super
  end
end
[](key) click to toggle source
# File lib/charty/table.rb, line 54
def [](key)
  case key
  when Array
    @adapter[nil, key]
  else
    key = case key
          when Symbol
            key
          else
            String.try_convert(key).to_sym
          end
    if @column_cache.key?(key)
      @column_cache[key]
    else
      @column_cache[key] = @adapter[nil, key]
    end
  end
end
[]=(key, values) click to toggle source
# File lib/charty/table.rb, line 73
def []=(key, values)
  case key
  when Array
    raise ArgumentError,
          "Substituting multiple keys is not supported"
  when Symbol
    # do nothing
  else
    key = key.to_str.to_sym
  end
  @adapter[key] = values
end
drop_na() click to toggle source
# File lib/charty/table.rb, line 123
def drop_na
  @adapter.drop_na || self
end
each() { |data| ... } click to toggle source
# File lib/charty/table.rb, line 113
def each
  return to_enum(__method__) unless block_given?
  data = to_a
  i, n = 0, data.size
  while i < n
    yield data[i]
    i += 1
  end
end
empty?() click to toggle source
# File lib/charty/table.rb, line 50
def empty?
  length == 0
end
group_by(grouper, sort: true, drop_na: true) click to toggle source
# File lib/charty/table.rb, line 86
def group_by(grouper, sort: true, drop_na: true)
  adapter.group_by(self, grouper, sort, drop_na)
end
to_a(x=nil, y=nil, z=nil) click to toggle source
# File lib/charty/table.rb, line 90
def to_a(x=nil, y=nil, z=nil)
  case
  when defined?(Daru::DataFrame) && table.kind_of?(Daru::DataFrame)
    table.map(&:to_a)
  when defined?(Numo::NArray) && table.kind_of?(Numo::NArray)
    table.to_a
  when defined?(NMatrix) && table.kind_of?(NMatrix)
    table.to_a
  when defined?(ActiveRecord::Relation) && table.kind_of?(ActiveRecord::Relation)
    if z && x && y
      [table.pluck(x), table.pluck(y), table.pluck(z)]
    elsif x && y
      [table.pluck(x), table.pluck(y)]
    else
      raise ArgumentError, "column_names is required to convert to_a from ActiveRecord::Relation"
    end
  when table.kind_of?(Array)
    table
  else
    raise ArgumentError, "unsupported object: #{table.inspect}"
  end
end