Class: Slingshot::Results::Collection
- Inherits:
-
Object
- Object
- Slingshot::Results::Collection
- Includes:
- Enumerable, Pagination
- Defined in:
- lib/slingshot/results/collection.rb
Instance Attribute Summary collapse
-
#facets ⇒ Object
readonly
Returns the value of attribute facets.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(response, options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #size ⇒ Object
- #to_ary ⇒ Object
Methods included from Pagination
#current_page, #next_page, #previous_page, #total_entries, #total_pages
Constructor Details
#initialize(response, options = {}) ⇒ Collection
Returns a new instance of Collection.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/slingshot/results/collection.rb', line 10 def initialize(response, ={}) @options = @time = response['took'].to_i @total = response['hits']['total'].to_i @results = response['hits']['hits'].map do |h| if Configuration.wrapper == Hash then h else document = {} # Update the document with content and ID document = h['_source'] ? document.update( h['_source'] || {} ) : document.update( h['fields'] || {} ) document.update( {'id' => h['_id']} ) # Update the document with meta information ['_score', '_version', 'sort', 'highlight'].each { |key| document.update( {key => h[key]} || {} ) } object = Configuration.wrapper.new(document) # TODO: Figure out how to circumvent mass assignment protection for id in ActiveRecord object.id = h['_id'] if object.respond_to?(:id=) # TODO: Figure out how mark record as "not new record" in ActiveRecord object.instance_variable_set(:@new_record, false) if object.respond_to?(:new_record?) object end end @facets = response['facets'] end |
Instance Attribute Details
#facets ⇒ Object (readonly)
Returns the value of attribute facets.
8 9 10 |
# File 'lib/slingshot/results/collection.rb', line 8 def facets @facets end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/slingshot/results/collection.rb', line 8 def @options end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
8 9 10 |
# File 'lib/slingshot/results/collection.rb', line 8 def results @results end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
8 9 10 |
# File 'lib/slingshot/results/collection.rb', line 8 def time @time end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
8 9 10 |
# File 'lib/slingshot/results/collection.rb', line 8 def total @total end |
Instance Method Details
#each(&block) ⇒ Object
37 38 39 |
# File 'lib/slingshot/results/collection.rb', line 37 def each(&block) @results.each(&block) end |
#empty? ⇒ Boolean
41 42 43 |
# File 'lib/slingshot/results/collection.rb', line 41 def empty? @results.empty? end |
#size ⇒ Object
45 46 47 |
# File 'lib/slingshot/results/collection.rb', line 45 def size @results.size end |
#to_ary ⇒ Object
49 50 51 |
# File 'lib/slingshot/results/collection.rb', line 49 def to_ary self end |