module ObjectTable::TableMethods

Constants

Util

Attributes

R[R]

Public Class Methods

new() click to toggle source
# File lib/object_table/table_methods.rb, line 17
def initialize
  @R = ObjectTable::BasicGrid
end

Public Instance Methods

==(other) click to toggle source
# File lib/object_table/table_methods.rb, line 21
def ==(other)
  return false unless other.is_a?(ObjectTable::TableMethods)
  return columns == other.columns
end
Also aliased as: eql?
[]=(name, value, *args)
Alias for: set_column
_get_sort_index(columns) click to toggle source
# File lib/object_table/table_methods.rb, line 106
def _get_sort_index(columns)
  (0...nrows).zip(columns.map(&:to_a).transpose).sort_by(&:last).map(&:first)
end
apply(&block) click to toggle source
# File lib/object_table/table_methods.rb, line 73
def apply(&block)
  result = Util.apply_block(self, block)
  return result unless result.is_a? ObjectTable::BasicGrid
  __table_cls__.new(result)
end
clone() click to toggle source
# File lib/object_table/table_methods.rb, line 101
def clone
  cols = ObjectTable::BasicGrid[columns.map{|k, v| [k, v.clone]}]
  __table_cls__.new(cols)
end
each_row(*cols, row_factory: Struct) { |row| ... } click to toggle source
# File lib/object_table/table_methods.rb, line 110
def each_row(*cols, row_factory: Struct)
  return to_enum(:each_row, *cols, row_factory: row_factory) unless block_given?
  return if ncols == 0

  cls = nil
  if cols.empty?
    cls = row_factory.new(*colnames)
    cols = colnames
  end

  columns = cols.map{|c| get_column(c)}
  nrows.times do |i|
    row = columns.map{|c| c[false, i]}
    row = cls.new(*row) if cls
    yield row
  end
end
eql?(other)
Alias for: ==
group_by(*args, &block) click to toggle source
# File lib/object_table/table_methods.rb, line 83
def group_by(*args, &block)
  ObjectTable::Grouping.new(self, *args, &block)
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/object_table/table_methods.rb, line 93
def method_missing(meth, *args, &block)
  get_column(meth) or super
end
ncols() click to toggle source
# File lib/object_table/table_methods.rb, line 31
def ncols
  columns.keys.length
end
nrows() click to toggle source
# File lib/object_table/table_methods.rb, line 27
def nrows
  columns.empty? ? 0 : ObjectTable::Column.length_of(columns.first[1])
end
respond_to?(meth, include_all = false) click to toggle source
Calls superclass method
# File lib/object_table/table_methods.rb, line 97
def respond_to?(meth, include_all = false)
  super or has_column?(meth)
end
set_column(name, value, *args) click to toggle source
# File lib/object_table/table_methods.rb, line 41
def set_column(name, value, *args)
  column = get_column(name)
  new_column = column.nil?

  value = value.to_a if value.is_a?(Range)
  is_vector = (value.is_a?(Array) or value.is_a?(NArray))

  if new_column
    if is_vector and args.empty?
      value =  NArray.to_na(value)
      unless (value.shape[-1] or 0) == nrows
        raise ArgumentError.new("Expected size of last dimension to be #{nrows}, was #{value.shape[-1].inspect}")
      end

      args = [value.typecode] + value.shape[0...-1]
    end

    column = add_column(name, *args)
  end

  return column if column.empty? and (!is_vector or value.empty?)

  begin
    column[] = value
  rescue Exception => e
    pop_column(name) if new_column
    raise e
  end
end
Also aliased as: []=
sort_by(*keys) click to toggle source
# File lib/object_table/table_methods.rb, line 87
def sort_by(*keys)
  sort_index = _get_sort_index(keys)
  cols = ObjectTable::BasicGrid[columns.map{|k, v| [k, v[sort_index]]}]
  __table_cls__.new(cols)
end
where(&block) click to toggle source
# File lib/object_table/table_methods.rb, line 79
def where(&block)
  __view_cls__.new(self, &block)
end