Class: Inquisitio::Searcher
- Inherits:
-
Object
- Object
- Inquisitio::Searcher
show all
- Defined in:
- lib/inquisitio/searcher.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params = nil) {|_self| ... } ⇒ Searcher
Returns a new instance of Searcher.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/inquisitio/searcher.rb', line 12
def initialize(params = nil)
@params = params || {
criteria: [],
named_fields: {},
per: 10,
page: 1,
returns: [],
sort: {},
q_options: {},
expressions: {}
}
@failed_attempts = 0
yield(self) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Proxy everything to the results so that this this class transparently acts as an Array.
135
136
137
|
# File 'lib/inquisitio/searcher.rb', line 135
def method_missing(name, *args, &block)
results.to_a.send(name, *args, &block)
end
|
Instance Attribute Details
#options(value) ⇒ Object
Returns the value of attribute options.
10
11
12
|
# File 'lib/inquisitio/searcher.rb', line 10
def options
@options
end
|
#params ⇒ Object
Returns the value of attribute params.
10
11
12
|
# File 'lib/inquisitio/searcher.rb', line 10
def params
@params
end
|
Class Method Details
.method_missing(name, *args) ⇒ Object
6
7
8
|
# File 'lib/inquisitio/searcher.rb', line 6
def self.method_missing(name, *args)
Searcher.new.send(name, *args)
end
|
Instance Method Details
#expressions(value) ⇒ Object
93
94
95
96
97
|
# File 'lib/inquisitio/searcher.rb', line 93
def expressions(value)
clone do |s|
s.params[:expressions] = value
end
end
|
#ids ⇒ Object
32
33
34
|
# File 'lib/inquisitio/searcher.rb', line 32
def ids
@ids ||= map { |r| r['data']['id'].first }.flatten.map(&:to_i)
end
|
#page(value) ⇒ Object
111
112
113
114
115
|
# File 'lib/inquisitio/searcher.rb', line 111
def page(value)
clone do |s|
s.params[:page] = value.to_i
end
end
|
#parser(value) ⇒ Object
99
100
101
102
103
|
# File 'lib/inquisitio/searcher.rb', line 99
def parser(value)
clone do |s|
s.params[:q_parser] = value
end
end
|
#per(value) ⇒ Object
105
106
107
108
109
|
# File 'lib/inquisitio/searcher.rb', line 105
def per(value)
clone do |s|
s.params[:per] = value.to_i
end
end
|
#records ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/inquisitio/searcher.rb', line 36
def records
return @records unless @records.nil?
@records = []
klasses = {}
results.each do |result|
klass = result['data']['type'].first
id = result['data']['id'].first
klasses[klass] ||= []
klasses[klass] << id
end
objs = klasses.map { |klass_name, ids|
klass_name = klass_name.gsub('_', '::')
klass = klass_name.constantize
klass.where(id: ids)
}.flatten
results.each do |result|
klass_name = result['data']['type'].first
klass_name = klass_name.gsub('_', '::')
id = result['data']['id'].first
record = objs.select { |r|
r.class.name == klass_name && r.id == id.to_i
}.first
@records << record
end
return @records
end
|
#returns(*value) ⇒ Object
117
118
119
120
121
122
123
124
125
|
# File 'lib/inquisitio/searcher.rb', line 117
def returns(*value)
clone do |s|
if value.is_a?(Array)
s.params[:returns] += value
else
s.params[:returns] << value
end
end
end
|
#search ⇒ Object
28
29
30
|
# File 'lib/inquisitio/searcher.rb', line 28
def search
results
end
|
#sort(value) ⇒ Object
127
128
129
130
131
|
# File 'lib/inquisitio/searcher.rb', line 127
def sort(value)
clone do |s|
s.params[:sort].merge!(value)
end
end
|
#where(value) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/inquisitio/searcher.rb', line 67
def where(value)
clone do |s|
if value.is_a?(Array)
s.params[:criteria] += value
elsif value.is_a?(Hash)
value.each do |k, v|
k = k.to_sym
s.params[:named_fields][k] ||= []
if v.is_a?(Array)
s.params[:named_fields][k] = v
else
s.params[:named_fields][k] << v
end
end
else
s.params[:criteria] << value
end
end
end
|