Module: ObjectTable::TableMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Printable

#_format_rows, #_format_section, calc_column_widths, get_printable_column, #inspect

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



100
101
102
# File 'lib/object_table/table_methods.rb', line 100

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

Instance Attribute Details

#RObject (readonly)

Returns the value of attribute R.



8
9
10
# File 'lib/object_table/table_methods.rb', line 8

def R
  @R
end

Instance Method Details

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



13
14
15
16
# File 'lib/object_table/table_methods.rb', line 13

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

#_apply_block(&block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/object_table/table_methods.rb', line 78

def _apply_block(&block)
  if block.arity == 0
    result = instance_eval &block
  else
    result = block.call(self)
  end
end

#_get_sort_index(columns) ⇒ Object



113
114
115
# File 'lib/object_table/table_methods.rb', line 113

def _get_sort_index(columns)
  (0...nrows).zip(columns.map(&:to_a).transpose).sort_by(&:last).map(&:first)
end

#apply(&block) ⇒ Object



71
72
73
74
75
76
# File 'lib/object_table/table_methods.rb', line 71

def apply(&block)
  result = _apply_block(&block)

  return result unless result.is_a? ObjectTable::BasicGrid
  __table_cls__.new(result)
end

#cloneObject



108
109
110
111
# File 'lib/object_table/table_methods.rb', line 108

def clone
  cols = ObjectTable::BasicGrid[columns.map{|k, v| [k, v.clone]}]
  __table_cls__.new(cols)
end

#colnamesObject



19
20
21
# File 'lib/object_table/table_methods.rb', line 19

def colnames
  columns.keys
end

#group_by(*args, &block) ⇒ Object



90
91
92
# File 'lib/object_table/table_methods.rb', line 90

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

#initializeObject



9
10
11
# File 'lib/object_table/table_methods.rb', line 9

def initialize
  @R = ObjectTable::BasicGrid
end

#ncolsObject



27
28
29
# File 'lib/object_table/table_methods.rb', line 27

def ncols
  columns.keys.length
end

#nrowsObject



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

def nrows
  columns.empty? ? 0 : (columns.values.first.shape[-1] or 0)
end

#pop_column(name) ⇒ Object



67
68
69
# File 'lib/object_table/table_methods.rb', line 67

def pop_column(name)
  columns.delete name
end

#respond_to?(meth, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/object_table/table_methods.rb', line 104

def respond_to?(meth, include_all = false)
  super or has_column?(meth)
end

#set_column(name, value, *args) ⇒ Object Also known as: []=



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/object_table/table_methods.rb', line 36

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



94
95
96
97
98
# File 'lib/object_table/table_methods.rb', line 94

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



86
87
88
# File 'lib/object_table/table_methods.rb', line 86

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