Class: Axiom::Relation::Operation::Sorted

Inherits:
Axiom::Relation show all
Includes:
Unary
Defined in:
lib/axiom/relation/operation/sorted.rb,
lib/axiom/relation/operation/sorted/direction.rb,
lib/axiom/relation/operation/sorted/direction_set.rb

Overview

A class representing a sorted relation

Direct Known Subclasses

Reverse

Defined Under Namespace

Modules: Methods Classes: Ascending, Descending, Direction, DirectionSet

Instance Attribute Summary collapse

Attributes included from Operation::Unary

#operand

Attributes inherited from Axiom::Relation

#header

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Axiom::Relation

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

Methods included from Visitable

#accept

Constructor Details

#initialize(operand, directions) ⇒ 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 an Sorted

Parameters:



52
53
54
55
# File 'lib/axiom/relation/operation/sorted.rb', line 52

def initialize(operand, directions)
  super(operand)
  @directions = directions
end

Instance Attribute Details

#directionsOperation::Sorted::DirectionSet (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 sort order



17
18
19
# File 'lib/axiom/relation/operation/sorted.rb', line 17

def directions
  @directions
end

Class Method Details

.new(operand, directions) ⇒ Sorted

Instantiate a new Sorted

Examples:

sorted = Sorted.new(operand, directions)

Parameters:

Returns:



32
33
34
35
36
37
38
39
40
# 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

Instance Method Details

#delete(other) ⇒ Sorted

Delete a relation from the Sorted

Examples:

new_relation = sorted.delete(other)

Parameters:

Returns:



102
103
104
105
# 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| ... } ⇒ self

Iterate over each tuple in the set

Examples:

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

Yields:

  • (tuple)

Yield Parameters:

  • tuple (Tuple)

    each tuple in the set

Returns:

  • (self)


71
72
73
74
75
# 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) ⇒ Sorted

Insert a relation into the Sorted

Examples:

new_relation = sorted.insert(other)

Parameters:

Returns:



87
88
89
90
# 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