module Babik::QuerySet::Limitable

Limit functionality of QuerySet

Public Instance Methods

[](param) click to toggle source

Configure a limit this QuerySet @param 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.

@raise RuntimeError 'Invalid limit passed to query: <VALUE>' If param is not a Range or Integer. @return [QuerySet, ActiveRecord::Base] QuerySet if a slice was passe as parameter,

otherwise an ActiveRecord model.
# 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?() click to toggle source

Inform if at least one record is matched by this QuerySet @return [Boolean] True if at least one record matches the conditions of the QuerySet, false otherwise.

# 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) click to toggle source

Return an element at an index, otherwise:

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

  • Raise an IndexError exception

@param index [Integer] Position of the element want to return. No negative number is allowed. @param default_value [Object] 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).

@raise [IndexError] When there is no default value

# 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) click to toggle source

Configure a limit this QuerySet @param size [Integer] Number of elements to be selected. @param offset [Integer] Position where the selection will start. By default is 0. No negative number is allowed. @return [QuerySet] Reference to this QuerySet.

# File lib/babik/queryset/mixins/limitable.rb, line 46
def limit!(size, offset = 0)
  @_limit = Babik::QuerySet::Limit.new(size, offset)
  self
end
limit?() click to toggle source

Inform if this QuerySet is limited @return [Boolean] true if this QuerySet has a limit, false otherwise.

# File lib/babik/queryset/mixins/limitable.rb, line 53
def limit?
  @_limit && true
end
unlimit!() click to toggle source

Destroy the current limit of this QuerySet @return [QuerySet] Reference to this QuerySet.

# File lib/babik/queryset/mixins/limitable.rb, line 59
def unlimit!
  @_limit = nil
  self
end

Private Instance Methods

limit_integer!(position) click to toggle source

Get one element at a determined position @param position [Integer] Position of the element to be returned. @api private @return [ActiveRecord::Base, nil] ActiveRecord::Base if exists a record in that position, nil otherwise.

# File lib/babik/queryset/mixins/limitable.rb, line 70
def limit_integer!(position)
  @_limit = Babik::QuerySet::Limit.new(1, position)
  self.first
end
limit_range!(param) click to toggle source

Get a QuerySet with a slice of the original QuerySet @param param [Range] first_element..last_element will be selected. @api private @return [QuerySet] QuerySet with a slice of the caller QuerySet.

# File lib/babik/queryset/mixins/limitable.rb, line 79
def limit_range!(param)
  offset = param.min
  size = param.max.to_i - param.min.to_i
  @_limit = Babik::QuerySet::Limit.new(size, offset)
  self
end