class Axiom::Tuple
A set of objects representing a unique fact in a relation
Attributes
The tuple data
@return [Hash]
@api private
The tuple header
@return [Header]
@api private
Public Class Methods
Initialize a Tuple
@param [Header] header
the tuple header
@param [#to_ary] data
the tuple data
@return [undefined]
@api private
# File lib/axiom/tuple.rb, line 46 def initialize(header, data) @header = header @to_ary = self.class.freezer.call(data) @data = Hash[header.zip(@to_ary)] end
Private Class Methods
Coerce an Array-like object into a Tuple
@param [Header] header
the tuple header
@param [Tuple, to_ary
] object
the tuple or tuple data
@return [Tuple]
@api private
# File lib/axiom/tuple.rb, line 200 def self.coerce(header, object) if object.kind_of?(Tuple) || header.size != object.to_ary.size object else new(header, object) end end
Public Instance Methods
Extend a tuple with function results
@example
new_tuple = tuple.extend(header, [func1, func2])
@param [Header] header
the attributes to include in the tuple
@param [Array<Object>] extensions
the functions to extend the tuple with
@return [Tuple]
@api public
# File lib/axiom/tuple.rb, line 94 def extend(header, extensions) join( header, extensions.map { |extension| Function.extract_value(extension, self) } ) end
Lookup a value in the tuple given an attribute
@example
value = tuple.call(attribute)
@param [Attribute] attribute
@return [Object]
@api public
# File lib/axiom/tuple.rb, line 62 def fetch(attribute) data.fetch(header.fetch(attribute)) end
Display the tuple data in a human readable form
@example
tuple.inspect # => data as a String
@return [String]
@api public
# File lib/axiom/tuple.rb, line 172 def inspect to_hash.inspect end
Append values to the tuple and return a new tuple
@example
new_tuple = tuple.join(header, [1, 2, 3])
@param [Header] header
the attributes to include in the tuple
@param [Array] values
the values to append
@return [Tuple]
@api public
# File lib/axiom/tuple.rb, line 128 def join(header, values) self.class.new(header, to_ary + values) end
Return the predicate matching the tuple
@return [Function]
@api private
# File lib/axiom/tuple.rb, line 137 def predicate header.reduce(Function::Proposition::Tautology.instance) do |predicate, attribute| predicate.and(attribute.eq(attribute.call(self))) end end
Return a tuple with only the specified attributes
@example
new_tuple = tuple.project(header)
@param [Header] header
the attributes to include in the tuple
@return [Tuple]
@api public
# File lib/axiom/tuple.rb, line 77 def project(header) self.class.new(header, data.values_at(*header)) end
Rename a tuple to use a new header
@example
new_tuple = tuple.rename(header)
@param [Header] header
@return [Tuple]
@api public
# File lib/axiom/tuple.rb, line 111 def rename(header) self.class.new(header, to_ary) end
The number of attributes
@return [Integer]
@api public
# File lib/axiom/tuple.rb, line 160 def size header.size end
Coerce the tuple into a Hash
@example
tuple.to_hash # => data as a Hash
@return [Hash{Symbol => Object}]
@api public
# File lib/axiom/tuple.rb, line 151 def to_hash Hash[data.map { |attribute, value| [attribute.name, value] }] end