Class: BaseFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_finder/base_finder.rb

Overview

Class Base finder

Direct Known Subclasses

AbstractFinder

Constant Summary collapse

DEFAULT_PER_PAGE =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filters) ⇒ BaseFinder

Returns a new instance of BaseFinder.



13
14
15
# File 'lib/abstract_finder/base_finder.rb', line 13

def initialize(filters)
  @filters = filters
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



5
6
7
# File 'lib/abstract_finder/base_finder.rb', line 5

def collection
  @collection
end

Class Method Details

.call(*args) ⇒ Object



7
8
9
10
11
# File 'lib/abstract_finder/base_finder.rb', line 7

def self.call(*args)
  instance = new(*args)
  instance.call
  instance
end

Instance Method Details

#callObject



17
18
19
20
21
# File 'lib/abstract_finder/base_finder.rb', line 17

def call
  load_collection

  self
end

#metaObject



37
38
39
40
41
42
43
44
# File 'lib/abstract_finder/base_finder.rb', line 37

def meta
  {
    page: page,
    per_page: per_page,
    total: @total,
    next_page: (@total - (page * per_page + per_page)).positive?
  }
end

#pageObject



23
24
25
# File 'lib/abstract_finder/base_finder.rb', line 23

def page
  filter_by(:page)&.to_i || 1
end

#per_pageObject



27
28
29
# File 'lib/abstract_finder/base_finder.rb', line 27

def per_page
  filter_by(:per_page)&.to_i || DEFAULT_PER_PAGE
end

#totalObject



31
32
33
34
35
# File 'lib/abstract_finder/base_finder.rb', line 31

def total
  return 0 if @collection.blank?

  @total ||= @collection.size
end