Class: Elos::Criteria

Inherits:
Result
  • Object
show all
Defined in:
lib/elos/criteria.rb

Instance Method Summary collapse

Methods inherited from Result

#each, #pluck

Constructor Details

#initialize(params:, query_builder_class:, klass:) ⇒ Criteria

Returns a new instance of Criteria.



4
5
6
7
8
9
# File 'lib/elos/criteria.rb', line 4

def initialize(params:, query_builder_class:, klass:)
  @client = Elos.client
  @params = params
  @query_builder_class = query_builder_class
  @klass = klass
end

Instance Method Details

#current_pageObject



37
38
39
# File 'lib/elos/criteria.rb', line 37

def current_page
  @params[:page]
end

#first!Object



59
60
61
62
63
# File 'lib/elos/criteria.rb', line 59

def first!
  r = first
  raise Elos::Errors::NotFound unless r
  r
end

#first_page?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/elos/criteria.rb', line 45

def first_page?
  current_page == 1
end

#includes(*names) ⇒ Object



27
28
29
30
# File 'lib/elos/criteria.rb', line 27

def includes(*names)
  @includes = names
  self
end

#last_page?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/elos/criteria.rb', line 49

def last_page?
  current_page == total_pages
end

#page(page) ⇒ Object



11
12
13
14
# File 'lib/elos/criteria.rb', line 11

def page(page)
  @params[:page] = page
  self
end

#per(per = nil) ⇒ Object Also known as: limit



16
17
18
19
20
21
22
23
# File 'lib/elos/criteria.rb', line 16

def per(per = nil)
  if per
    @params[:per] = per
    self
  else
    @params[:per]
  end
end

#queryObject



65
66
67
# File 'lib/elos/criteria.rb', line 65

def query
  @query_builder_class.new(@params.merge(class: @klass)).wrap_build
end

#to_a!Object



53
54
55
56
57
# File 'lib/elos/criteria.rb', line 53

def to_a!
  r = to_a
  raise Elos::Errors::NotFound if r.empty?
  r
end

#total_countObject



32
33
34
35
# File 'lib/elos/criteria.rb', line 32

def total_count
  result
  @total_count
end

#total_pagesObject



41
42
43
# File 'lib/elos/criteria.rb', line 41

def total_pages
  [0, total_count - 1].max / @params[:per] + 1
end