Class: OMF::OML::Tuple

Inherits:
Base::LObject
  • Object
show all
Defined in:
lib/omf_oml/tuple.rb

Overview

This class represents a tuple with an associated schema. It provides various methods to access the tuple elements.

NOTE: Do not store the tuple itself, but make a copy as the instance may be reused over various rows by the sender.

Use OmlTuple if the schema is an OML one. OmlTuple has additional convenience methods.

Direct Known Subclasses

OmlTuple

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, schema) ⇒ Tuple

Returns a new instance of Tuple.



68
69
70
71
72
73
74
# File 'lib/omf_oml/tuple.rb', line 68

def initialize(name, schema)
  @stream_name = name
  @schema = OmlSchema.create(schema)
  @raw = []
  @on_new_tuple_procs = {}
  super name
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



65
66
67
# File 'lib/omf_oml/tuple.rb', line 65

def schema
  @schema
end

#stream_nameObject (readonly)

Returns the value of attribute stream_name.



66
67
68
# File 'lib/omf_oml/tuple.rb', line 66

def stream_name
  @stream_name
end

Instance Method Details

#[](name_or_index) ⇒ Object

Return a specific element of the tuple identified either by it’s name, or its col index



28
29
30
# File 'lib/omf_oml/tuple.rb', line 28

def [](name_or_index)
  @schema.cast_col(name_or_index, @raw)
end

#create_table(table_name, opts = {}) ⇒ Object

Return a table (more precisely an OmlTable instance) fed from the content of this tuple stream.

table_name - Name of table opts -

All options defined for OmlTable#create


54
55
56
57
58
59
60
61
62
63
# File 'lib/omf_oml/tuple.rb', line 54

def create_table(table_name, opts = {})
  table = OmlTable.create(table_name, @schema, opts)
  id = -1
  on_new_tuple(table) do |t|
    row = @schema.cast_row(@raw, true)
    #puts "ROW>> #{row.inspect}"
    table.add_row(row)
  end
  table
end

#on_new_tuple(key = :_, &proc) ⇒ Object

Register a proc to be called when a new tuple arrived



92
93
94
95
96
97
98
# File 'lib/omf_oml/tuple.rb', line 92

def on_new_tuple(key = :_, &proc)
  if proc
    @on_new_tuple_procs[key] = proc
  else
    @on_new_tuple_procs.delete key
  end
end

#parse_tuple(els) ⇒ Object

Parse the array of strings into the proper typed vector elements

NOTE: We assume that each element is only called at most once, with some never called. We therefore delay type-casting to the get function without keeping the casted values (would increase lookup time)



83
84
85
86
87
88
# File 'lib/omf_oml/tuple.rb', line 83

def parse_tuple(els)
  @raw = els
  @on_new_tuple_procs.each_value do |proc|
    proc.call(self)
  end
end

#select(*col_names) ⇒ Object

Return an array including the values for the names elements given as parameters.



40
41
42
43
44
45
# File 'lib/omf_oml/tuple.rb', line 40

def select(*col_names)
  r = @raw
  col_names.map do |n|
    self[n]
  end
end

#to_aObject

Return the elements of the tuple as an array



33
34
35
# File 'lib/omf_oml/tuple.rb', line 33

def to_a()
  @schema.cast_row(@raw)
end