Module: Axiom::Relation::Operation::Limit::Methods

Defined in:
lib/axiom/relation/operation/limit.rb

Constant Summary collapse

DEFAULT_ONE_BLOCK =

Default block used in #one

-> {}
ONE_LIMIT =

Maximum number of tuples to take in #one

2

Instance Method Summary collapse

Instance Method Details

#first(limit = 1) ⇒ Limit

Return a relation with the first n tuples

Examples:

with no limit

limited_relation = relation.first

with a limit

limited_relation = relation.first(5)

Parameters:

  • limit (Integer) (defaults to: 1)

    optional number of tuples from the beginning of the relation

Returns:



188
189
190
# File 'lib/axiom/relation/operation/limit.rb', line 188

def first(limit = 1)
  take(limit)
end

#last(limit = 1) ⇒ Limit

Return a relation with the last n tuples

Examples:

with no limit

limited_relation = relation.last

with a limit

limited_relation = relation.last(5)

Parameters:

  • limit (Integer) (defaults to: 1)

    optional number of tuples from the end of the relation

Returns:



206
207
208
# File 'lib/axiom/relation/operation/limit.rb', line 206

def last(limit = 1)
  reverse.take(limit).reverse
end

#one(&block) ⇒ Tuple

Return a tuple if the relation contains exactly one tuple

Examples:

without a block

tuple = relation.one

with a block

tuple = relation.one { ... }

Yield Returns:

  • (Object)

Returns:

Raises:



228
229
230
231
232
233
234
# 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) ⇒ Limit

Return a relation with n tuples

Examples:

limited_relation = relation.take(5)

Parameters:

  • limit (Integer)

    the maximum number of tuples in the limited relation

Returns:



170
171
172
# File 'lib/axiom/relation/operation/limit.rb', line 170

def take(limit)
  Limit.new(self, limit)
end