class Axiom::Relation::Operation::Sorted

A class representing a sorted relation

Attributes

directions[R]

The relation sort order

@return [Operation::Sorted::DirectionSet]

@api private

Public Class Methods

new(operand, directions) click to toggle source

Instantiate a new Sorted

@example

sorted = Sorted.new(operand, directions)

@param [Relation] operand

the relation to sort

@param [DirectionSet, Header, Array<Direction, Attribute>] directions

the directions to sort tuples in

@return [Sorted]

@api public

Calls superclass method Axiom::Relation::Operation::Unary::new
# File lib/axiom/relation/operation/sorted.rb, line 32
def self.new(operand, directions)
  header     = operand.header
  directions = DirectionSet.coerce(directions) do |direction|
    header[direction] unless direction.kind_of?(Direction)
  end
  new_directions = directions | header - directions.attributes
  directions     = new_directions if new_directions != directions
  super
end
new(operand, directions) click to toggle source

Initialize an Sorted

@param [Relation] operand

the relation to sort

@param [DirectionSet, Array<Direction, Attribute>] directions

the directions to sort tuples in

@return [undefined]

@api private

Calls superclass method Axiom::Relation::Operation::Unary::new
# File lib/axiom/relation/operation/sorted.rb, line 52
def initialize(operand, directions)
  super(operand)
  @directions = directions
end

Public Instance Methods

delete(other) click to toggle source

Delete a relation from the Sorted

@example

new_relation = sorted.delete(other)

@param [Relation] other

@return [Sorted]

@api public

# File lib/axiom/relation/operation/sorted.rb, line 102
def delete(other)
  assert_matching_directions(other, DELETED)
  operand.delete(other.operand).sort_by(directions)
end
each() { |tuple| ... } click to toggle source

Iterate over each tuple in the set

@example

sorted = Sorted.new(operand, directions)
sorted.each { |tuple| ... }

@yield [tuple]

@yieldparam [Tuple] tuple

each tuple in the set

@return [self]

@api public

# File lib/axiom/relation/operation/sorted.rb, line 71
def each
  return to_enum unless block_given?
  directions.sort_tuples(operand).each { |tuple| yield tuple }
  self
end
insert(other) click to toggle source

Insert a relation into the Sorted

@example

new_relation = sorted.insert(other)

@param [Relation] other

@return [Sorted]

@api public

# File lib/axiom/relation/operation/sorted.rb, line 87
def insert(other)
  assert_matching_directions(other, INSERTED)
  operand.insert(other.operand).sort_by(directions)
end

Private Instance Methods

assert_matching_directions(other, event) click to toggle source

Assert that the other relation has matching directions

@param [Relation] other

@param [String] event

@return [undefined]

@raise [SortedMismatchError]

raised when inserting a relation does not have matching directions

@api private

# File lib/axiom/relation/operation/sorted.rb, line 121
def assert_matching_directions(other, event)
  unless other.respond_to?(:directions) && directions.eql?(other.directions)
    fail SortedMismatchError, "other relation must have matching directions to be #{event}"
  end
end