Class: Cloudsearchable::Query
- Inherits:
-
Object
- Object
- Cloudsearchable::Query
- Includes:
- Enumerable
- Defined in:
- lib/cloudsearchable.rb
Overview
Wraps a Cloudsearchable::QueryChain, provides methods to execute and reify a query into search result objects
Instance Attribute Summary collapse
-
#class ⇒ Object
readonly
Returns the value of attribute class.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #found_count ⇒ Object
-
#initialize(clazz, domain, identity_field) ⇒ Query
constructor
A new instance of Query.
-
#materialize!(*args) ⇒ Object
Pass through to Cloudsearchable::Domain#materialize!, then retrieve objects from database TODO: this does NOT preserve order!.
Constructor Details
#initialize(clazz, domain, identity_field) ⇒ Query
Returns a new instance of Query.
171 172 173 174 175 176 |
# File 'lib/cloudsearchable.rb', line 171 def initialize(clazz, domain, identity_field) @query = Cloudsearchable::QueryChain.new(domain, fatal_warnings: Cloudsearchable.config.fatal_warnings) @class = clazz @query.returning(identity_field) @identity_field = identity_field end |
Instance Attribute Details
#class ⇒ Object (readonly)
Returns the value of attribute class.
162 163 164 |
# File 'lib/cloudsearchable.rb', line 162 def class @class end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
162 163 164 |
# File 'lib/cloudsearchable.rb', line 162 def query @query end |
Instance Method Details
#each(&block) ⇒ Object
195 196 197 198 199 200 |
# File 'lib/cloudsearchable.rb', line 195 def each &block # Returns an enumerator return enum_for(__method__) unless block_given? materialize! @results.respond_to?(:each) ? @results.each { |o| yield o } : [@results].send(:each, &block) end |
#found_count ⇒ Object
202 203 204 |
# File 'lib/cloudsearchable.rb', line 202 def found_count query.found_count end |
#materialize!(*args) ⇒ Object
Pass through to Cloudsearchable::Domain#materialize!, then retrieve objects from database TODO: this does NOT preserve order!
188 189 190 191 192 193 |
# File 'lib/cloudsearchable.rb', line 188 def materialize!(*args) @results ||= begin record_ids = @query.map{|result_hit| result_hit['data'][@identity_field.to_s].first}.reject{|r| r.nil?} @class.send(@class.materialize_method, record_ids) end end |