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

get_printable_column, get_printable_line_numbers, #inspect

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

#_get_sort_index(columns) ⇒ Object



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

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

#apply(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/object_table/table_methods.rb', line 69

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

  if result.is_a? ObjectTable::BasicGrid
    result = __table_cls__.new(result)
  end
  result
end

#cloneObject



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

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



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

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



65
66
67
# File 'lib/object_table/table_methods.rb', line 65

def pop_column(name)
  columns.delete name
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(meth)
  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
# File 'lib/object_table/table_methods.rb', line 36

def set_column(name, value, *args)
  column = get_column(name)
  value = value.to_a if value.is_a?(Range)

  if column
    return (column[] = value)
  end

  if (value.is_a?(Array) or value.is_a?(NArray)) 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)
  return column if value.is_a?(NArray) and value.empty?

  begin
    column[] = value
  rescue Exception => e
    pop_column(name)
    raise e
  end
end

#sort_by(*keys) ⇒ Object



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

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



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

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