Module: Ambition::Limit

Included in:
Ambition
Defined in:
lib/ambition/lib/ambition/limit.rb

Instance Method Summary collapse

Instance Method Details

#[](offset, limit = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ambition/lib/ambition/limit.rb', line 8

def [](offset, limit = nil)
  return first(offset, limit) if limit

  if offset.is_a? Range
    limit  = offset.end
    limit -= 1 if offset.exclude_end?
    first(offset.first, limit - offset.first)
  else
    first(offset, 1)
  end
end

#first(limit = 1, offset = nil) ⇒ Object



3
4
5
6
# File 'lib/ambition/lib/ambition/limit.rb', line 3

def first(limit = 1, offset = nil)
  query_context.add LimitProcessor.new(limit, offset)
  find(limit == 1 ? :first : :all, query_context.to_hash)
end