Module: Babik::QuerySet::Limitable

Included in:
AbstractBase
Defined in:
lib/babik/queryset/mixins/limitable.rb

Overview

Limit functionality of QuerySet

Instance Method Summary collapse

Instance Method Details

#[](param) ⇒ QuerySet, ActiveRecord::Base

Configure a limit this QuerySet

Parameters:

  • param (Range, Integer)

    If it is a range, first_element..last_element will be selected. If it is an integer, the element in that position will be returned. No negative number is allowed.

Returns:

  • (QuerySet, ActiveRecord::Base)

    QuerySet if a slice was passe as parameter, otherwise an ActiveRecord model.



15
16
17
18
# File 'lib/babik/queryset/mixins/limitable.rb', line 15

def [](param)
  raise "Invalid limit passed to query: #{param}" unless [Range, Integer].include?(param.class)
  self.clone.send("limit_#{param.class.to_s.downcase}!", param)
end

#exists?Boolean

Inform if at least one record is matched by this QuerySet

Returns:

  • (Boolean)

    True if at least one record matches the conditions of the QuerySet, false otherwise.



22
23
24
25
26
# File 'lib/babik/queryset/mixins/limitable.rb', line 22

def exists?
  element = self.fetch(0, false)
  return true if element
  false
end

#fetch(index, default_value = nil) ⇒ Object

Return an element at an index, otherwise:

  • Return a default value if it has been passed as second argument.

  • Raise an IndexError exception

Parameters:

  • index (Integer)

    Position of the element want to return. No negative number is allowed.

  • default_value (Object) (defaults to: nil)

    Anything that will be returned if no record is found at the index position. By default it takes a nil value (in that case, it will raise the IndexError exception).

Raises:

  • (IndexError)

    When there is no default value



35
36
37
38
39
40
# File 'lib/babik/queryset/mixins/limitable.rb', line 35

def fetch(index, default_value = nil)
  element = self.[](index)
  return element if element
  return default_value unless default_value.nil?
  raise IndexError, "Index #{index} outside of QuerySet bounds"
end

#limit!(size, offset = 0) ⇒ QuerySet

Configure a limit this QuerySet

Parameters:

  • size (Integer)

    Number of elements to be selected.

  • offset (Integer) (defaults to: 0)

    Position where the selection will start. By default is 0. No negative number is allowed.

Returns:

  • (QuerySet)

    Reference to this QuerySet.



46
47
48
49
# File 'lib/babik/queryset/mixins/limitable.rb', line 46

def limit!(size, offset = 0)
  @_limit = Babik::QuerySet::Limit.new(size, offset)
  self
end

#limit?Boolean

Inform if this QuerySet is limited

Returns:

  • (Boolean)

    true if this QuerySet has a limit, false otherwise.



53
54
55
# File 'lib/babik/queryset/mixins/limitable.rb', line 53

def limit?
  @_limit && true
end

#unlimit!QuerySet

Destroy the current limit of this QuerySet

Returns:

  • (QuerySet)

    Reference to this QuerySet.



59
60
61
62
# File 'lib/babik/queryset/mixins/limitable.rb', line 59

def unlimit!
  @_limit = nil
  self
end