module Axiom::Relation::Operation::Limit::Methods

Constants

DEFAULT_ONE_BLOCK

Default block used in one

ONE_LIMIT

Maximum number of tuples to take in one

Public Instance Methods

first(limit = 1) click to toggle source

Return a relation with the first n tuples

@example with no limit

limited_relation = relation.first

@example with a limit

limited_relation = relation.first(5)

@param [Integer] limit

optional number of tuples from the beginning of the relation

@return [Limit]

@api public

# File lib/axiom/relation/operation/limit.rb, line 188
def first(limit = 1)
  take(limit)
end
last(limit = 1) click to toggle source

Return a relation with the last n tuples

@example with no limit

limited_relation = relation.last

@example with a limit

limited_relation = relation.last(5)

@param [Integer] limit

optional number of tuples from the end of the relation

@return [Limit]

@api public

# File lib/axiom/relation/operation/limit.rb, line 206
def last(limit = 1)
  reverse.take(limit).reverse
end
one(&block) click to toggle source

Return a tuple if the relation contains exactly one tuple

@example without a block

tuple = relation.one

@example with a block

tuple = relation.one { ... }

@yieldreturn [Object]

@return [Tuple]

@raise [NoTuplesError]

raised if no tuples are returned

@raise [ManyTuplesError]

raised if more than one tuple is returned

@api public

# File lib/axiom/relation/operation/limit.rb, line 228
def one(&block)
  block ||= DEFAULT_ONE_BLOCK
  tuples = take(ONE_LIMIT).to_a
  assert_no_more_than_one_tuple(tuples.size)
  tuples.first or block.yield or
    fail NoTuplesError, 'one tuple expected, but was an empty set'
end
take(limit) click to toggle source

Return a relation with n tuples

@example

limited_relation = relation.take(5)

@param [Integer] limit

the maximum number of tuples in the limited relation

@return [Limit]

@api public

# File lib/axiom/relation/operation/limit.rb, line 170
def take(limit)
  Limit.new(self, limit)
end

Private Instance Methods

assert_no_more_than_one_tuple(size) click to toggle source

Assert no more than one tuple is returned

@return [undefined]

@raise [ManyTuplesError]

raised if more than one tuple is returned

@api private

# File lib/axiom/relation/operation/limit.rb, line 246
def assert_no_more_than_one_tuple(size)
  if size > 1
    fail(
      ManyTuplesError,
      "one tuple expected, but set contained #{count} tuples"
    )
  end
end