class Axiom::Tuple

A set of objects representing a unique fact in a relation

Attributes

data[R]

The tuple data

@return [Hash]

@api private

header[R]

The tuple header

@return [Header]

@api private

to_ary[R]

Convert the Tuple into an Array

@example

array = tuple.to_ary

@return [Array]

@api public

Public Class Methods

new(header, data) click to toggle source

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(header, object) click to toggle source

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(header, extensions) click to toggle source

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
fetch(attribute) click to toggle source

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
inspect() click to toggle source

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
join(header, values) click to toggle source

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
predicate() click to toggle source

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
project(header) click to toggle source

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(header) click to toggle source

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
size() click to toggle source

The number of attributes

@return [Integer]

@api public

# File lib/axiom/tuple.rb, line 160
def size
  header.size
end
to_hash() click to toggle source

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

Private Instance Methods

coerce(object) click to toggle source

Coerce an Array-like object into a Tuple

@param [Tuple, to_ary] object

the tuple or tuple data

@return [Tuple]

@api private

# File lib/axiom/tuple.rb, line 186
def coerce(object)
  self.class.coerce(header, object)
end