Class: Axiom::Relation

Inherits:
Object
  • Object
show all
Includes:
Visitable, Enumerable
Defined in:
lib/axiom/relation.rb,
lib/axiom/relation/base.rb,
lib/axiom/relation/keys.rb,
lib/axiom/relation/empty.rb,
lib/axiom/relation/proxy.rb,
lib/axiom/relation/header.rb,
lib/axiom/relation/materialized.rb,
lib/axiom/relation/operation/set.rb,
lib/axiom/relation/operation/limit.rb,
lib/axiom/relation/operation/order.rb,
lib/axiom/relation/operation/unary.rb,
lib/axiom/relation/operation/binary.rb,
lib/axiom/relation/operation/offset.rb,
lib/axiom/relation/operation/reverse.rb,
lib/axiom/relation/operation/deletion.rb,
lib/axiom/relation/operation/insertion.rb,
lib/axiom/relation/operation/combination.rb,
lib/axiom/relation/operation/order/direction.rb,
lib/axiom/relation/operation/order/direction_set.rb

Overview

Abstract base class for Relation operations

Defined Under Namespace

Modules: Operation, Proxy Classes: Base, Empty, Header, Keys, Materialized

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Visitable

#accept

Constructor Details

#initialize(header, tuples) ⇒ 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 Relation

Parameters:

  • header (Header, #to_ary)

    the relation header

  • tuples (Enumerable)

    the relation tuples



72
73
74
75
# File 'lib/axiom/relation.rb', line 72

def initialize(header, tuples)
  @header = Header.coerce(header)
  @tuples = freeze_object(tuples)
end

Instance Attribute Details

#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 relation header

Returns:



15
16
17
# File 'lib/axiom/relation.rb', line 15

def header
  @header
end

Class Method Details

.new(*args) ⇒ Relation

Instantiate a new Relation

Examples:

of a materialized Array based relation

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

of a materialized Set based relation

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

of a non-materialized Enumerator based relation

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

Parameters:

  • args (Array(Header, Enumerable))

Returns:



53
54
55
56
57
58
59
60
# File 'lib/axiom/relation.rb', line 53

def self.new(*args)
  last = args.last
  if superclass.equal?(Object) && last.respond_to?(:size) && last.size.kind_of?(Integer)
    Materialized.new(*args)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Compare the relation with other relation for equivalency

Examples:

relation == other  # => true or false

Parameters:

  • other (Relation)

    the other relation to compare with

Returns:

  • (Boolean)


202
203
204
205
206
# File 'lib/axiom/relation.rb', line 202

def ==(other)
  other = coerce(other)
  header == other.header &&
  to_set == other.to_set
end

#[](name) ⇒ Attribute

Lookup an Attribute in the header given an attribute name

Examples:

attribute = relation[name]

Parameters:

  • name (#to_sym)

    the attribute name

Returns:



88
89
90
# File 'lib/axiom/relation.rb', line 88

def [](name)
  header[name]
end

#directionsOperation::Order::DirectionSet

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 relation sort order



30
31
32
# File 'lib/axiom/relation.rb', line 30

def directions
  Operation::Order::DirectionSet::EMPTY
end

#each {|tuple| ... } ⇒ self

Iterate over each tuple in the set

Examples:

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

Yields:

  • (tuple)

Yield Parameters:

  • tuple (Tuple)

    each tuple in the set

Returns:

  • (self)


106
107
108
109
110
111
112
113
114
# File 'lib/axiom/relation.rb', line 106

def each
  return to_enum unless block_given?
  seen = Hash.new
  tuples.each do |tuple|
    tuple = Tuple.coerce(header, tuple)
    yield seen[tuple] = tuple unless seen.key?(tuple)
  end
  self
end

#empty?Boolean

Test if there are no tuples

Examples:

relation.empty?  # => true or false

Returns:

  • (Boolean)


216
217
218
# File 'lib/axiom/relation.rb', line 216

def empty?
  none?
end

#include?(tuple) ⇒ Boolean

Test if the tuple exists in the relation

Examples:

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

Parameters:

Returns:

  • (Boolean)


187
188
189
# File 'lib/axiom/relation.rb', line 187

def include?(tuple)
  to_set.include?(tuple)
end

#materializeMaterialized

Return a relation with each tuple materialized

Examples:

materialized = relation.materialize

Returns:



161
162
163
# File 'lib/axiom/relation.rb', line 161

def materialize
  Materialized.new(header, to_a, directions)
end

#materialized?false

Return false for a non-Materialized relation

Examples:

relation.materialized?  # => false

Returns:

  • (false)


173
174
175
# File 'lib/axiom/relation.rb', line 173

def materialized?
  false
end

#oneTuple

Return a tuple if the relation contains exactly one tuple

Examples:

tuple = relation.one

Returns:

Raises:



129
130
131
132
133
# File 'lib/axiom/relation.rb', line 129

def one
  tuples = to_a
  assert_exactly_one_tuple(tuples.size)
  tuples.first
end

#replace(other) ⇒ Relation::Operation::Insertion

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.

Examples:

replacement = relation.delete(other)

Parameters:

  • other (Enumerable)

Returns:



148
149
150
151
# File 'lib/axiom/relation.rb', line 148

def replace(other)
  other = coerce(other)
  delete(difference(other)).insert(other.difference(self))
end