lib/veritas/tuple.rb in veritas-0.0.3 vs lib/veritas/tuple.rb in veritas-0.0.4

- old
+ new

@@ -1,5 +1,7 @@ +# encoding: utf-8 + module Veritas # A set of objects representing a unique fact in a relation class Tuple include Immutable @@ -21,11 +23,11 @@ # @return [undefined] # # @api private def initialize(header, data) @header = header - @data = data.to_ary + @data = Hash[header.zip(data.to_ary)].freeze end # Lookup a value in the tuple given an attribute # # @example @@ -35,12 +37,11 @@ # # @return [Object] # # @api public def [](attribute) - index = header.index(attribute) - to_ary.at(index) if index + @data[header[attribute]] end # Return a tuple with only the specified attributes # # @example @@ -51,11 +52,11 @@ # # @return [Tuple] # # @api public def project(header) - self.class.new(header, header.map { |attribute| self[attribute] }) + self.class.new(header, @data.values_at(*header)) end # Append values to the tuple and return a new tuple # # @example @@ -78,18 +79,21 @@ # @example # new_tuple = tuple.extend(header, [ func1, func2 ]) # # @param [Header] header # the attributes to include in the tuple - # @param [Array<#call>] extensions + # @param [Array<Object>] extensions # the functions to extend the tuple with # # @return [Tuple] # # @api public def extend(header, extensions) - join(header, extensions.map { |extension| extension.call(self) }) + join( + header, + extensions.map { |extension| Function.extract_value(extension, self) } + ) end # Convert the Tuple into an Array # # @example @@ -97,11 +101,11 @@ # # @return [Array] # # @api public def to_ary - @data + @data.values_at(*header).freeze end # Compare the tuple with other tuple for equivalency # # @example @@ -145,11 +149,11 @@ # # @return [Fixnum] # # @api public def hash - self.class.hash ^ header.hash ^ to_ary.hash + self.class.hash ^ header.hash ^ @data.hash end # Return a string representing the tuple data # # @example @@ -174,9 +178,9 @@ # @api private def self.coerce(header, object) object.kind_of?(Tuple) ? object : new(header, object) end - memoize :hash + memoize :hash, :to_ary end # class Tuple end # module Veritas