Class: SearchObject::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/search_object/search.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, params, actions) ⇒ Search

Returns a new instance of Search.



17
18
19
20
21
# File 'lib/search_object/search.rb', line 17

def initialize(scope, params, actions)
  @scope    = scope
  @actions  = actions
  @params   = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/search_object/search.rb', line 3

def params
  @params
end

Class Method Details

.build_for(config, options) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
# File 'lib/search_object/search.rb', line 6

def build_for(config, options)
  scope   = options.fetch(:scope) { config[:scope] && config[:scope].call }
  filters = Helper.stringify_keys(options.fetch(:filters, {}))
  params  = config[:defaults].merge Helper.select_keys(filters, config[:actions].keys)

  raise MissingScopeError unless scope

  new scope, params, config[:actions]
end

Instance Method Details

#count(context) ⇒ Object



34
35
36
# File 'lib/search_object/search.rb', line 34

def count(context)
  query(context).count
end

#param(name) ⇒ Object



23
24
25
# File 'lib/search_object/search.rb', line 23

def param(name)
  @params[name]
end

#query(context) ⇒ Object



27
28
29
30
31
32
# File 'lib/search_object/search.rb', line 27

def query(context)
  @params.inject(@scope) do |scope, (name, value)|
    new_scope = context.instance_exec scope, value, &@actions[name]
    new_scope || scope
  end
end