Class: Axiom::Tuple

Inherits:
Object
  • Object
show all
Extended by:
Aliasable
Defined in:
lib/axiom/tuple.rb

Overview

A set of objects representing a unique fact in a relation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Aliasable

inheritable_alias

Constructor Details

#initialize(header, data) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a Tuple

Parameters:

  • header (Header)

    the tuple header

  • data (#to_ary)

    the tuple data



36
37
38
39
# File 'lib/axiom/tuple.rb', line 36

def initialize(header, data)
  @header = header
  @data   = Hash[header.zip(data)]
end

Instance Attribute Details

#dataHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The tuple data

Returns:

  • (Hash)


24
25
26
# File 'lib/axiom/tuple.rb', line 24

def data
  @data
end

#headerHeader (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The tuple header

Returns:

  • (Header)


17
18
19
# File 'lib/axiom/tuple.rb', line 17

def header
  @header
end

Instance Method Details

#call(attribute) ⇒ Object

Lookup a value in the tuple given an attribute

Examples:

value = tuple.call(attribute)

Parameters:

Returns:

  • (Object)


51
52
53
# File 'lib/axiom/tuple.rb', line 51

def call(attribute)
  data.fetch(header.call(attribute))
end

#extend(header, extensions) ⇒ Tuple

Extend a tuple with function results

Examples:

new_tuple = tuple.extend(header, [ func1, func2 ])

Parameters:

  • header (Header)

    the attributes to include in the tuple

  • extensions (Array<Object>)

    the functions to extend the tuple with

Returns:



100
101
102
103
104
105
# File 'lib/axiom/tuple.rb', line 100

def extend(header, extensions)
  join(
    header,
    extensions.map { |extension| Function.extract_value(extension, self) }
  )
end

#join(header, values) ⇒ Tuple

Append values to the tuple and return a new tuple

Examples:

new_tuple = tuple.join(header, [ 1, 2, 3 ])

Parameters:

  • header (Header)

    the attributes to include in the tuple

  • values (Array)

    the values to append

Returns:



83
84
85
# File 'lib/axiom/tuple.rb', line 83

def join(header, values)
  self.class.new(header, to_ary + values)
end

#predicateFunction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the predicate matching the tuple

Returns:



112
113
114
115
116
# File 'lib/axiom/tuple.rb', line 112

def predicate
  header.reduce(Function::Proposition::Tautology.instance) do |predicate, attribute|
    predicate.and(attribute.eq(attribute.call(self)))
  end
end

#project(header) ⇒ Tuple

Return a tuple with only the specified attributes

Examples:

new_tuple = tuple.project(header)

Parameters:

  • header (Header)

    the attributes to include in the tuple

Returns:



66
67
68
# File 'lib/axiom/tuple.rb', line 66

def project(header)
  self.class.new(header, data.values_at(*header))
end

#to_aryArray

Convert the Tuple into an Array

Examples:

array = tuple.to_ary

Returns:

  • (Array)


126
127
128
# File 'lib/axiom/tuple.rb', line 126

def to_ary
  data.values_at(*header).freeze
end