Module: ObjectTable::TableMethods

Extended by:
Forwardable
Includes:
Joining, Printing, Stacking
Included in:
ObjectTable, ViewMethods
Defined in:
lib/object_table/table_methods.rb

Constant Summary collapse

Util =
ObjectTable::Util

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Stacking

process_stackable_grid, #stack, stack_segments

Methods included from Joining

copy_column, #join

Methods included from Printing

calc_column_widths, format_column, format_rows, format_section, #inspect, split_column_lines

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



93
94
95
# File 'lib/object_table/table_methods.rb', line 93

def method_missing(meth, *args, &block)
  get_column(meth) or super
end

Instance Attribute Details

#RObject (readonly)

Returns the value of attribute R.



16
17
18
# File 'lib/object_table/table_methods.rb', line 16

def R
  @R
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



21
22
23
24
# File 'lib/object_table/table_methods.rb', line 21

def ==(other)
  return false unless other.is_a?(ObjectTable::TableMethods)
  return columns == other.columns
end

#_get_sort_index(columns) ⇒ Object



106
107
108
# 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) ⇒ Object



73
74
75
76
77
# 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

#cloneObject



101
102
103
104
# 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) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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

#group_by(*args, &block) ⇒ Object



83
84
85
# File 'lib/object_table/table_methods.rb', line 83

def group_by(*args, &block)
  ObjectTable::Grouping.new(self, *args, &block)
end

#initializeObject



17
18
19
# File 'lib/object_table/table_methods.rb', line 17

def initialize
  @R = ObjectTable::BasicGrid
end

#ncolsObject



31
32
33
# File 'lib/object_table/table_methods.rb', line 31

def ncols
  columns.keys.length
end

#nrowsObject



27
28
29
# 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) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# 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) ⇒ Object Also known as: []=



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#sort_by(*keys) ⇒ Object



87
88
89
90
91
# 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) ⇒ Object



79
80
81
# File 'lib/object_table/table_methods.rb', line 79

def where(&block)
  __view_cls__.new(self, &block)
end