Class: Slingshot::Results::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Pagination
Defined in:
lib/slingshot/results/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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={})
  @options = 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

#facetsObject (readonly)

Returns the value of attribute facets.



8
9
10
# File 'lib/slingshot/results/collection.rb', line 8

def facets
  @facets
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/slingshot/results/collection.rb', line 8

def options
  @options
end

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'lib/slingshot/results/collection.rb', line 8

def results
  @results
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/slingshot/results/collection.rb', line 8

def time
  @time
end

#totalObject (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

Returns:

  • (Boolean)


41
42
43
# File 'lib/slingshot/results/collection.rb', line 41

def empty?
  @results.empty?
end

#sizeObject



45
46
47
# File 'lib/slingshot/results/collection.rb', line 45

def size
  @results.size
end

#to_aryObject



49
50
51
# File 'lib/slingshot/results/collection.rb', line 49

def to_ary
  self
end