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

- old
+ new

@@ -11,10 +11,17 @@ # @return [Header] # # @api private attr_reader :header + # The tuple data + # + # @return [Hash] + # + # @api private + attr_reader :data + # Initialize a Tuple # # @param [Header] header # the tuple header # @param [#to_ary] data @@ -37,11 +44,11 @@ # # @return [Object] # # @api public def [](attribute) - @data[header[attribute]] + data.fetch(header[attribute]) end # Return a tuple with only the specified attributes # # @example @@ -52,11 +59,11 @@ # # @return [Tuple] # # @api public def project(header) - self.class.new(header, @data.values_at(*header)) + self.class.new(header, data.values_at(*header)) end # Append values to the tuple and return a new tuple # # @example @@ -92,20 +99,31 @@ header, extensions.map { |extension| Function.extract_value(extension, self) } ) end + # Return the predicate matching the tuple + # + # @return [Function] + # + # @api private + def predicate + header.reduce(Function::Proposition::Tautology.instance) do |predicate, attribute| + predicate.and(attribute.eq(self[attribute])) + end + end + # Convert the Tuple into an Array # # @example # array = tuple.to_ary # # @return [Array] # # @api public def to_ary - @data.values_at(*header).freeze + data.values_at(*header).freeze end # Compare the tuple with other tuple for equivalency # # @example @@ -116,14 +134,12 @@ # # @return [Boolean] # # @api public def ==(other) - header = self.header - other = self.class.coerce(header, other) - header == other.header && - to_ary == other.project(header).to_ary + other = self.class.coerce(header, other) + data == other.data end # Compare the tuple with other tuple for equality # # @example @@ -134,14 +150,12 @@ # # @return [Boolean] # # @api public def eql?(other) - header = self.header instance_of?(other.class) && - header.eql?(other.header) && - to_ary.eql?(other.project(header).to_ary) + data.eql?(other.data) end # Return the hash of the tuple # # @example @@ -149,23 +163,23 @@ # # @return [Fixnum] # # @api public def hash - self.class.hash ^ header.hash ^ @data.hash + self.class.hash ^ data.hash end # Return a string representing the tuple data # # @example - # tuple.inspect # => "[1, 2, 3]" + # tuple.inspect # => "{<Attribute::Integer name: id>=>1}" # # @return [String] # # @api public def inspect - to_ary.inspect + data.inspect end # Coerce an Array-like object into a Tuple # # @param [Header] header @@ -178,9 +192,9 @@ # @api private def self.coerce(header, object) object.kind_of?(Tuple) ? object : new(header, object) end - memoize :hash, :to_ary + memoize :hash, :predicate, :to_ary end # class Tuple end # module Veritas