Module: Babik::QuerySet::Filterable

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

Overview

Functionality related to the DELETE operation

Instance Method Summary collapse

Instance Method Details

#exclude!(filter) ⇒ QuerySet

Exclude objects according to some criteria.

Returns:



9
10
11
12
# File 'lib/babik/queryset/mixins/filterable.rb', line 9

def exclude!(filter)
  _filter(filter, 'exclusion')
  self
end

#filter!(filter) ⇒ QuerySet

Select objects according to some criteria.

Parameters:

  • filter (Array, Hash)

    if array, it is considered an disjunction (OR clause), if a hash, it is considered a conjunction (AND clause).

Returns:



18
19
20
21
# File 'lib/babik/queryset/mixins/filterable.rb', line 18

def filter!(filter)
  _filter(filter, 'inclusion')
  self
end

#get(filter) ⇒ ActiveRecord::Base

Get an single element

Parameters:

  • filter (Array, Hash)

    if array, it is considered an disjunction (OR clause), if a hash, it is considered a conjunction (AND clause).

Returns:

  • (ActiveRecord::Base)

    object that matches the filter.

Raises:

  • (RuntimeError)

    Exception: ‘Multiple objects returned’ if more than one object matches the condition. ‘Does not exist’ if no object match the conditions.



30
31
32
33
34
35
36
# File 'lib/babik/queryset/mixins/filterable.rb', line 30

def get(filter)
  result_ = self.filter(filter).all
  result_count = result_.count
  raise 'Does not exist' if result_count.zero?
  raise 'Multiple objects returned' if result_count > 1
  result_.first
end