lib/omf_oml/tuple.rb in omf_oml-1.0.0 vs lib/omf_oml/tuple.rb in omf_oml-1.1.0

- old
+ new

@@ -5,10 +5,11 @@ # By downloading or using this software you accept the terms and the liability disclaimer in the License. #------------------------------------------------------------------------------- require 'omf_base/lobject' require 'omf_oml' require 'omf_oml/schema' +require 'omf_oml/table' module OMF::OML # This class represents a tuple with an associated schema. # It provides various methods to access the tuple elements. @@ -23,51 +24,55 @@ # Return a specific element of the tuple identified either # by it's name, or its col index # def [](name_or_index) - @vprocs[name_or_index].call(@raw) + @schema.cast_col(name_or_index, @raw) end # Return the elements of the tuple as an array def to_a() - # res = [] - # r = @raw - # @schema.each do |col| - # res << @vprocs[col[:name]].call(r) - # end - # res @schema.cast_row(@raw) end # Return an array including the values for the names elements # given as parameters. # def select(*col_names) r = @raw - col_names.collect do |n| - p = @vprocs[n] - p ? p.call(r) : nil + col_names.map do |n| + self[n] end end + # 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 + # + 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 + attr_reader :schema attr_reader :stream_name 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 # Parse the array of strings into the proper typed vector elements # @@ -80,11 +85,11 @@ @on_new_tuple_procs.each_value do |proc| proc.call(self) end end - # Register a proc to be called when a new tuple arrived + # Register a proc to be called when a new tuple arrived # def on_new_tuple(key = :_, &proc) if proc @on_new_tuple_procs[key] = proc else @@ -92,24 +97,8 @@ end end protected - def process_schema(schema) - i = 0 - @vprocs = {} - schema.each_column do |col| # - name = col[:name] || raise("Ill-formed schema '#{schema}'") - type = col[:type] || raise("Ill-formed schema '#{schema}'") - @vprocs[name] = @vprocs[i] = case type - when :string - lambda do |r| r[i] end - when :float - lambda do |r| r[i].to_f end - else raise "Unrecognized Schema type '#{type}'" - end - i += 1 - end - end end # Tuple end # OMF::OML