class Axiom::Relation::Operation::Limit

A class representing a limited relation

Attributes

directions[R]

The relation sort order

@return [Operation::Sorted::DirectionSet]

@api private

limit[R]

Return the limit

@example

limit = limited_relation.limit

@return [Integer]

@api public

Public Class Methods

new(operand, limit) click to toggle source

Instantiate a new Limit

@example

limited_relation = Limit.new(operand, limit)

@param [Relation] operand

the relation to limit

@param [Integer] limit

the maximum number of tuples in the limited relation

@return [Limit]

@api public

Calls superclass method Axiom::Relation::Operation::Unary::new
# File lib/axiom/relation/operation/limit.rb, line 42
def self.new(operand, limit)
  assert_sorted_operand(operand)
  assert_valid_limit(limit)
  super
end
new(operand, limit) click to toggle source

Initialize a Limit

@param [Relation] operand

the relation to limit

@param [Integer] limit

the maximum number of tuples in the limited relation

@return [undefined]

@api private

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

Private Class Methods

assert_sorted_operand(operand) click to toggle source

Assert the operand is sorted

@param [Relation] operand

@return [undefined]

@raise [SortededRelationRequiredError]

raised if the operand is unsorted

@api private

# File lib/axiom/relation/operation/limit.rb, line 58
def self.assert_sorted_operand(operand)
  if operand.header.size != operand.directions.size
    fail SortededRelationRequiredError, 'can only limit a sorted operand'
  end
end
assert_valid_limit(limit) click to toggle source

Assert the limit is valid

@param [Integer] limit

@return [undefined]

@raise [InvalidLimitError]

raised if the limit is less than 0

@api private

# File lib/axiom/relation/operation/limit.rb, line 74
def self.assert_valid_limit(limit)
  if limit.nil? || limit < 0
    fail InvalidLimitError, "limit must be greater than or equal to 0, but was #{limit.inspect}"
  end
end

Public Instance Methods

delete(*) click to toggle source

Raise an exception when deleting from the Limit

@example

limit.delete(other)  # => ImmutableRelationError raised

@return [undefined]

@raise [ImmutableRelationError]

raised when deleting from the limit

@api public

# File lib/axiom/relation/operation/limit.rb, line 147
def delete(*)
  fail ImmutableRelationError, 'deleting from a limit is impossible'
end
each() { |tuple| ... } click to toggle source

Iterate over each tuple in the set

@example

limited_relation = Limit.new(operand, limit)
limited_relation.each { |tuple| ... }

@yield [tuple]

@yieldparam [Tuple] tuple

each tuple in the set

@return [self]

@api public

# File lib/axiom/relation/operation/limit.rb, line 112
def each
  return to_enum unless block_given?
  operand.each_with_index do |tuple, index|
    break if @limit == index
    yield tuple
  end
  self
end
insert(*) click to toggle source

Raise an exception when inserting into the Limit

@example

limit.insert(other)  # => ImmutableRelationError raised

@return [undefined]

@raise [ImmutableRelationError]

raised when inserting into the limit

@api public

# File lib/axiom/relation/operation/limit.rb, line 132
def insert(*)
  fail ImmutableRelationError, 'inserting into a limit is impossible'
end