module SnFoil::Searcher
@author Matthew Howes
@since 0.1.0
Constants
- VERSION
Attributes
scope[R]
Public Class Methods
new(scope: nil)
click to toggle source
# File lib/snfoil/searcher.rb, line 74 def initialize(scope: nil) raise SnFoil::Searcher::Error, "No default scope or model configured for #{self.class.name}" if !scope && !model @scope = scope || model.all end
Public Instance Methods
booleans(*fields)
click to toggle source
# File lib/snfoil/searcher.rb, line 54 def booleans(*fields) @snfoil_booleans ||= [] @snfoil_booleans |= fields.map(&:to_sym) end
filter(method = nil, **options, &block)
click to toggle source
# File lib/snfoil/searcher.rb, line 48 def filter(method = nil, **options, &block) raise SnFoil::Searcher::ArgumentError, 'filter requires either a method name or a block' if method.nil? && block.nil? (@snfoil_filters ||= []) << { method: method, block: block, if: options[:if], unless: options[:unless] } end
filters()
click to toggle source
# File lib/snfoil/searcher.rb, line 93 def filters self.class.snfoil_filters || [] end
inherited(subclass)
click to toggle source
Calls superclass method
# File lib/snfoil/searcher.rb, line 59 def inherited(subclass) super instance_variables.grep(/@snfoil_.+/).each do |i| subclass.instance_variable_set(i, instance_variable_get(i).dup) end end
model(klass = nil)
click to toggle source
# File lib/snfoil/searcher.rb, line 36 def model(klass = nil) raise SnFoil::Searcher::Error, "model already defined for #{self.class.name}" if @snfoil_model @snfoil_model = klass end
search(params = {})
click to toggle source
# File lib/snfoil/searcher.rb, line 80 def search(params = {}) params = transform_params_booleans(params) # this is required for params coming in from http-like sources filtered_scope = filter || scope # start usimg the default scope of the class or the filter method filtered_scope = apply_setup(filtered_scope, params) apply_filters(filtered_scope, params) end
setup(setup_method = nil, &setup_block)
click to toggle source
# File lib/snfoil/searcher.rb, line 42 def setup(setup_method = nil, &setup_block) raise SnFoil::Searcher::Error, "setup already defined for #{self.class.name}" if @snfoil_setup @snfoil_setup = setup_method || setup_block end
Private Instance Methods
apply_filter(i_filter, filtered_scope, params)
click to toggle source
# File lib/snfoil/searcher.rb, line 119 def apply_filter(i_filter, filtered_scope, params) return filtered_scope unless filter_valid?(i_filter, params) return send(i_filter[:method], filtered_scope, params) if i_filter[:method] instance_exec filtered_scope, params, &i_filter[:block] end
apply_filters(filtered_scope, params)
click to toggle source
# File lib/snfoil/searcher.rb, line 113 def apply_filters(filtered_scope, params) filters&.reduce(filtered_scope) do |i_scope, i_filter| apply_filter(i_filter, i_scope, params) end end
apply_setup(filtered_scope, params)
click to toggle source
# File lib/snfoil/searcher.rb, line 103 def apply_setup(filtered_scope, params) return filtered_scope if setup.nil? if setup.is_a?(Symbol) || setup.is_a?(String) send(setup, filtered_scope, params) else instance_exec filtered_scope, params, &setup end end
filter_valid?(i_filter, params)
click to toggle source
# File lib/snfoil/searcher.rb, line 127 def filter_valid?(i_filter, params) return false if !i_filter[:if].nil? && !i_filter[:if].call(params) return false if !i_filter[:unless].nil? && i_filter[:unless].call(params) true end
transform_params_booleans(params)
click to toggle source
# File lib/snfoil/searcher.rb, line 134 def transform_params_booleans(params) params.map do |key, value| value = if booleans.include?(key.to_sym) SnFoil::Searcher::Boolean.new.cast(value) else value end [key, value] end.to_h end