Class: Axiom::Algebra::Projection

Inherits:
Relation
  • Object
show all
Includes:
Relation::Operation::Unary
Defined in:
lib/axiom/algebra/projection.rb

Overview

Specify only specific attributes to keep in the relation

Defined Under Namespace

Modules: Methods

Instance Attribute Summary

Attributes included from Operation::Unary

#operand

Attributes inherited from Relation

#header

Instance Method Summary collapse

Methods inherited from Relation

#==, #[], #directions, #empty?, #include?, #materialize, #materialized?, new, #replace

Methods included from Visitable

#accept

Constructor Details

#initialize(operand, attributes) ⇒ 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 Projection

Parameters:

  • operand (Relation)

    the relation to project

  • attributes (#to_ary)

    the attributes to keep in the header



21
22
23
24
# File 'lib/axiom/algebra/projection.rb', line 21

def initialize(operand, attributes)
  super(operand)
  @header = @header.project(attributes)
end

Instance Method Details

#delete(other) ⇒ Projection

Delete a relation from the Projection

Examples:

new_relation = projection.delete(other)

Parameters:

Returns:

Raises:



86
87
88
89
90
# File 'lib/axiom/algebra/projection.rb', line 86

def delete(other)
  other = coerce(other)
  assert_equivalent_headers(other)
  operand.delete(extend_other(other)).project(header)
end

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

Iterate over each tuple in the set

Examples:

projection = Projection.new(operand, attributes)
projection.each { |tuple| ... }

Yields:

  • (tuple)

Yield Parameters:

  • tuple (Tuple)

    each tuple in the set

Returns:

  • (self)


40
41
42
43
44
45
46
47
48
# File 'lib/axiom/algebra/projection.rb', line 40

def each
  return to_enum unless block_given?
  seen = {}
  operand.each do |tuple|
    tuple = tuple.project(header)
    yield seen[tuple] = tuple unless seen.key?(tuple)
  end
  self
end

#insert(other) ⇒ Projection

Insert a relation into the Projection

Examples:

new_relation = projection.insert(other)

Parameters:

Returns:

Raises:



66
67
68
69
70
71
# File 'lib/axiom/algebra/projection.rb', line 66

def insert(other)
  other = coerce(other)
  assert_removed_attributes_optional
  assert_equivalent_headers(other)
  operand.insert(extend_other(other)).project(header)
end