class Pagoid::AdapterRouter

Attributes

pageable[RW]

Public Class Methods

new(pageable) click to toggle source
# File lib/pagoid/adapter_router.rb, line 7
def initialize(pageable)
  self.pageable = pageable
end

Public Instance Methods

route() click to toggle source
# File lib/pagoid/adapter_router.rb, line 13
def route
  chosen_adapter = configured || route_table.find { |routeable| useable? routeable }
  raise RouterError, "Could not find a suitable Pagoid Adapter" unless chosen_adapter
  load_dependencies chosen_adapter
  constantize(pagoided(chosen_adapter)).new pageable
end

Private Instance Methods

configured() click to toggle source
# File lib/pagoid/adapter_router.rb, line 22
def configured
end
constantize(adapter) click to toggle source
# File lib/pagoid/adapter_router.rb, line 47
def constantize(adapter)
  Object.module_eval(adapter)
end
load_dependencies(adapter) click to toggle source
# File lib/pagoid/adapter_router.rb, line 29
def load_dependencies(adapter)
  require "pagoid/#{underscore(adapter)}"
end
pagoided(adapter) click to toggle source
# File lib/pagoid/adapter_router.rb, line 25
def pagoided(adapter)
  "::Pagoid::#{adapter}"
end
route_table() click to toggle source
# File lib/pagoid/adapter_router.rb, line 51
def route_table
  %w[
    Kaminari
    WillPaginate
  ]
end
underscore(adapter_name) click to toggle source
# File lib/pagoid/adapter_router.rb, line 33
def underscore(adapter_name)
  adapter_name.gsub(/::/, '/')
  .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  .gsub(/([a-z\d])([A-Z])/,'\1_\2')
  .tr("-", "_")
  .downcase
end
useable?(adapter) click to toggle source
# File lib/pagoid/adapter_router.rb, line 41
def useable?(adapter)
  constantize "::#{adapter}"
rescue NameError
  false
end