Module: Babik::QuerySet::Limitable
- Included in:
- AbstractBase
- Defined in:
- lib/babik/queryset/mixins/limitable.rb
Overview
Limit functionality of QuerySet
Instance Method Summary collapse
-
#[](param) ⇒ QuerySet, ActiveRecord::Base
Configure a limit this QuerySet.
-
#exists? ⇒ Boolean
Inform if at least one record is matched by this QuerySet.
-
#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.
-
#limit!(size, offset = 0) ⇒ QuerySet
Configure a limit this QuerySet.
-
#limit? ⇒ Boolean
Inform if this QuerySet is limited.
-
#unlimit! ⇒ QuerySet
Destroy the current limit of this QuerySet.
Instance Method Details
#[](param) ⇒ QuerySet, ActiveRecord::Base
Configure a limit this QuerySet
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
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
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
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
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
59 60 61 62 |
# File 'lib/babik/queryset/mixins/limitable.rb', line 59 def unlimit! @_limit = nil self end |