class BaseFinder

Class Base finder

Constants

DEFAULT_PER_PAGE

Attributes

collection[R]

Public Class Methods

call(*args) click to toggle source
# File lib/abstract_finder/base_finder.rb, line 7
def self.call(*args)
  instance = new(*args)
  instance.call
  instance
end
new(filters) click to toggle source
# File lib/abstract_finder/base_finder.rb, line 13
def initialize(filters)
  @filters = filters
end

Public Instance Methods

call() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 17
def call
  load_collection

  self
end
meta() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 43
def meta
  {
    page: page,
    per_page: per_page,
    total: @total,
    next_page: next_page?
  }
end
next_page?() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 37
def next_page?
  (@total - (page.next * per_page)).positive?
rescue StandardError
  false
end
page() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 23
def page
  filter_by(:page)&.to_i || 1
end
per_page() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 27
def per_page
  filter_by(:per_page)&.to_i || DEFAULT_PER_PAGE
end
total() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 31
def total
  return 0 if @collection.blank?

  @total ||= @collection.size
end

Protected Instance Methods

filter_by(param_name) click to toggle source

Internal: filter value for parameter

param_name - name of filters param

Returns String

# File lib/abstract_finder/base_finder.rb, line 70
def filter_by(param_name)
  @filters[param_name]
end
filtered_by?(param_name) click to toggle source

Internal: check if filter by param specified

param_name - name of filters param

Returns Boolean

# File lib/abstract_finder/base_finder.rb, line 62
def filtered_by?(param_name)
  filter_by(param_name).present?
end
load_collection() click to toggle source
# File lib/abstract_finder/base_finder.rb, line 54
def load_collection
  @collection = nil
end