class Axiom::Relation

Abstract base class for Relation operations

Attributes

header[R]

The relation header

@return [Header]

@api private

tuples[R]

The relation tuples

@return [Enumerable]

@api private

Public Class Methods

new(*args) click to toggle source

Instantiate a new Relation

@example of a materialized Array based relation

array    = [[1], [2], [3]]
relation = Relation.new([[:id, Integer]], array)

@example of a materialized Set based relation

set      = Set[[1], [2], [3]]
relation = Relation.new([[:id, Integer]], set)

@example of a non-materialized Enumerator based relation

enumerator = [[1], [2], [3]].each
relation   = Relation.new([[:id, Integer]], enumerator)

@param [Array(Header, Enumerable)] args

@return [Relation]

@api public

Calls superclass method
# File lib/axiom/relation.rb, line 53
def self.new(*args)
  if equal?(Relation) && materialized?(args[1])
    Materialized.new(*args)
  else
    super
  end
end
new(header, tuples) click to toggle source

Initialize a Relation

@param [Header, to_ary] header

the relation header

@param [Enumerable] tuples

the relation tuples

@return [undefined]

@api private

# File lib/axiom/relation.rb, line 86
def initialize(header, tuples)
  @header = Header.coerce(header)
  @tuples = tuples
end

Private Class Methods

coerce(header, object) click to toggle source

Coerce an Enumerable into a Relation

@param [Header] header

the header to use when initializing a Relation

@param [Enumerable] object

the object to coerce

@return [Relation]

@api private

# File lib/axiom/relation.rb, line 240
def self.coerce(header, object)
  if object.kind_of?(Relation) || !object.kind_of?(Enumerable)
    object
  else
    Relation.new(header, object)
  end
end
materialized?(tuples) click to toggle source

Test if the tuples are materialized

When tuples are nil, it means there are no tuples so it is the equivalent of specifying [] for the tuples.

@param [Enumerable, nil] tuples

@return [Boolean]

@api private

# File lib/axiom/relation.rb, line 71
def self.materialized?(tuples)
  tuples.nil? || tuples.respond_to?(:size) && tuples.size.kind_of?(Integer)
end

Public Instance Methods

==(other) click to toggle source

Compare the relation with other relation for equivalency

@example

relation == other  # => true or false

@param [Relation] other

the other relation to compare with

@return [Boolean]

@api public

# File lib/axiom/relation.rb, line 197
def ==(other)
  other = coerce(other)
  other.kind_of?(Relation) &&
  header == other.header   &&
  to_set == other.to_set
end
[](name) click to toggle source

Lookup an Attribute in the header given an attribute name

@example

attribute = relation[name]

@param [#to_sym] name

the attribute name

@return [Attribute]

@api public

# File lib/axiom/relation.rb, line 102
def [](name)
  header[name]
end
directions() click to toggle source

The relation sort order

@return [Operation::Sorted::DirectionSet]

@api private

# File lib/axiom/relation.rb, line 30
def directions
  Operation::Sorted::DirectionSet::EMPTY
end
each() { |seen = tuple| ... } click to toggle source

Iterate over each tuple in the set

@example

relation = Relation.new(header, tuples)
relation.each { |tuple| ... }

@yield [tuple]

@yieldparam [Tuple] tuple

each tuple in the set

@return [self]

@api public

# File lib/axiom/relation.rb, line 120
def each
  return to_enum unless block_given?
  seen = {}
  tuples.each do |tuple|
    tuple = Tuple.coerce(header, tuple)
    yield seen[tuple] = tuple unless seen.key?(tuple)
  end
  self
end
empty?() click to toggle source

Test if there are no tuples

@example

relation.empty?  # => true or false

@return [Boolean]

@api public

# File lib/axiom/relation.rb, line 212
def empty?
  none?
end
include?(tuple) click to toggle source

Test if the tuple exists in the relation

@example

relation.include?(tuple)  # => true or false

@param [Tuple] tuple

@return [Boolean]

@api public

# File lib/axiom/relation.rb, line 182
def include?(tuple)
  to_set.include?(tuple)
end
materialize() click to toggle source

Return a relation with each tuple materialized

@example

materialized = relation.materialize

@return [Materialized]

@api public

# File lib/axiom/relation.rb, line 156
def materialize
  Materialized.new(header, to_a, directions)
end
materialized?() click to toggle source

Return false for a non-Materialized relation

@example

relation.materialized?  # => false

@return [false]

@api public

# File lib/axiom/relation.rb, line 168
def materialized?
  false
end
replace(other) click to toggle source

Return a relation that represents a replacement of a relation

Delete the tuples from the relation that are not in the other relation, then insert only new tuples.

@example

replacement = relation.delete(other)

@param [Enumerable] other

@return [Relation::Operation::Insertion]

@api public

# File lib/axiom/relation.rb, line 143
def replace(other)
  other = coerce(other)
  delete(difference(other)).insert(other.difference(self))
end

Private Instance Methods

coerce(object) click to toggle source

Coerce an Enumerable into a Relation

@param [Enumerable] object

the object to coerce

@return [Relation]

@api private

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