Class: OMF::OML::Tuple
- Inherits:
-
Base::LObject
- Object
- Base::LObject
- OMF::OML::Tuple
- 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
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#stream_name ⇒ Object
readonly
Returns the value of attribute stream_name.
Instance Method Summary collapse
-
#[](name_or_index) ⇒ Object
Return a specific element of the tuple identified either by it’s name, or its col index.
-
#initialize(name, schema) ⇒ Tuple
constructor
A new instance of Tuple.
-
#on_new_tuple(key = :_, &proc) ⇒ Object
Register a proc to be called when a new tuple arrived.
-
#parse_tuple(els) ⇒ Object
Parse the array of strings into the proper typed vector elements.
-
#select(*col_names) ⇒ Object
Return an array including the values for the names elements given as parameters.
-
#to_a ⇒ Object
Return the elements of the tuple as an array.
Constructor Details
#initialize(name, schema) ⇒ Tuple
Returns a new instance of Tuple.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/omf_oml/tuple.rb', line 56 def initialize(name, schema) # if schema.kind_of? Array # schema = OmlSchema.new(schema) # end @stream_name = name @schema = OmlSchema.create(schema) @raw = [] # puts "SCHEMA: #{schema.inspect}" @on_new_tuple_procs = {} super name process_schema(@schema) end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
53 54 55 |
# File 'lib/omf_oml/tuple.rb', line 53 def schema @schema end |
#stream_name ⇒ Object (readonly)
Returns the value of attribute stream_name.
54 55 56 |
# File 'lib/omf_oml/tuple.rb', line 54 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
27 28 29 |
# File 'lib/omf_oml/tuple.rb', line 27 def [](name_or_index) @vprocs[name_or_index].call(@raw) end |
#on_new_tuple(key = :_, &proc) ⇒ Object
Register a proc to be called when a new tuple arrived
87 88 89 90 91 92 93 |
# File 'lib/omf_oml/tuple.rb', line 87 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)
78 79 80 81 82 83 |
# File 'lib/omf_oml/tuple.rb', line 78 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.
45 46 47 48 49 50 51 |
# File 'lib/omf_oml/tuple.rb', line 45 def select(*col_names) r = @raw col_names.collect do |n| p = @vprocs[n] p ? p.call(r) : nil end end |
#to_a ⇒ Object
Return the elements of the tuple as an array
32 33 34 35 36 37 38 39 40 |
# File 'lib/omf_oml/tuple.rb', line 32 def to_a() # res = [] # r = @raw # @schema.each do |col| # res << @vprocs[col[:name]].call(r) # end # res @schema.cast_row(@raw) end |