Class: Inquisitio::Searcher

Inherits:
Object
  • Object
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.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/inquisitio/searcher.rb', line 12

def initialize(params = nil)
  @params = params || {
    criteria: [],
    filters: {},
    per: 10,
    page: 1,
    returns: [],
    with: {},
    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.



142
143
144
# File 'lib/inquisitio/searcher.rb', line 142

def method_missing(name, *args, &block)
  results.to_a.send(name, *args, &block)
end

Instance Attribute Details

#options(value) ⇒ Object (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/inquisitio/searcher.rb', line 10

def options
  @options
end

#paramsObject (readonly)

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



94
95
96
97
98
# File 'lib/inquisitio/searcher.rb', line 94

def expressions(value)
  clone do |s|
    s.params[:expressions] = value
  end
end

#idsObject



33
34
35
# File 'lib/inquisitio/searcher.rb', line 33

def ids
  @ids ||= map { |r| r['data']['id'].first }.flatten.map(&:to_i)
end

#page(value) ⇒ Object



112
113
114
115
116
# File 'lib/inquisitio/searcher.rb', line 112

def page(value)
  clone do |s|
    s.params[:page] = value.to_i
  end
end

#parser(value) ⇒ Object



100
101
102
103
104
# File 'lib/inquisitio/searcher.rb', line 100

def parser(value)
  clone do |s|
    s.params[:q_parser] = value
  end
end

#per(value) ⇒ Object



106
107
108
109
110
# File 'lib/inquisitio/searcher.rb', line 106

def per(value)
  clone do |s|
    s.params[:per] = value.to_i
  end
end

#recordsObject



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
66
# File 'lib/inquisitio/searcher.rb', line 37

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



118
119
120
121
122
123
124
125
126
# File 'lib/inquisitio/searcher.rb', line 118

def returns(*value)
  clone do |s|
    if value.is_a?(Array)
      s.params[:returns] += value
    else
      s.params[:returns] << value
    end
  end
end

#searchObject



29
30
31
# File 'lib/inquisitio/searcher.rb', line 29

def search
  results
end

#sort(value) ⇒ Object



134
135
136
137
138
# File 'lib/inquisitio/searcher.rb', line 134

def sort(value)
  clone do |s|
    s.params[:sort].merge!(value)
  end
end

#where(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/inquisitio/searcher.rb', line 68

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[:filters][k] ||= []
        if v.is_a?(Array)
          s.params[:filters][k] = v
        else
          s.params[:filters][k] << v
        end
      end
    else
      s.params[:criteria] << value
    end
  end
end

#with(value) ⇒ Object



128
129
130
131
132
# File 'lib/inquisitio/searcher.rb', line 128

def with(value)
  clone do |s|
    s.params[:with].merge!(value)
  end
end