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



46
47
48
49
50
# 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

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

#to_aryArray (readonly)

Convert the Tuple into an Array

Examples:

array = tuple.to_ary

Returns:

  • (Array)


34
35
36
# File 'lib/axiom/tuple.rb', line 34

def to_ary
  @to_ary
end

Instance Method Details

#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:



94
95
96
97
98
99
# File 'lib/axiom/tuple.rb', line 94

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

#fetch(attribute) ⇒ Object

Lookup a value in the tuple given an attribute

Examples:

value = tuple.call(attribute)

Parameters:

Returns:

  • (Object)


62
63
64
# File 'lib/axiom/tuple.rb', line 62

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

#inspectString

Display the tuple data in a human readable form

Examples:

tuple.inspect  # => data as a String

Returns:

  • (String)


172
173
174
# File 'lib/axiom/tuple.rb', line 172

def inspect
  to_hash.inspect
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:



128
129
130
# File 'lib/axiom/tuple.rb', line 128

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:



137
138
139
140
141
# 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) ⇒ 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:



77
78
79
# File 'lib/axiom/tuple.rb', line 77

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

#rename(header) ⇒ Tuple

Rename a tuple to use a new header

Examples:

new_tuple = tuple.rename(header)

Parameters:

  • header (Header)

Returns:



111
112
113
# File 'lib/axiom/tuple.rb', line 111

def rename(header)
  self.class.new(header, to_ary)
end

#sizeInteger

The number of attributes

Returns:

  • (Integer)


160
161
162
# File 'lib/axiom/tuple.rb', line 160

def size
  header.size
end

#to_hashHash{Symbol => Object}

Coerce the tuple into a Hash

Examples:

tuple.to_hash  # => data as a Hash

Returns:

  • (Hash{Symbol => Object})


151
152
153
# File 'lib/axiom/tuple.rb', line 151

def to_hash
  Hash[data.map { |attribute, value| [attribute.name, value] }]
end