Class: Neoid::SearchSession

Inherits:
Object
  • Object
show all
Defined in:
lib/neoid/search_session.rb

Instance Method Summary collapse

Constructor Details

#initialize(response, *models) ⇒ SearchSession

Returns a new instance of SearchSession.



3
4
5
6
# File 'lib/neoid/search_session.rb', line 3

def initialize(response, *models)
  @response = response || []
  @models = models
end

Instance Method Details

#hitsObject



8
9
10
# File 'lib/neoid/search_session.rb', line 8

def hits
  @response.map { |x| Neography::Node.new(x) }
end

#idsObject



12
13
14
# File 'lib/neoid/search_session.rb', line 12

def ids
  @response.map { |x| x['data']['ar_id'] }
end

#resultsObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/neoid/search_session.rb', line 16

def results
  models_by_name = @models.inject({}) { |all, curr| all[curr.name] = curr; all }

  ids_by_klass = @response.inject({}) do |all, curr|
    klass_name = curr['data']['ar_type']
    (all[models_by_name[klass_name]] ||= []) << curr['data']['ar_id']
    all
  end

  ids_by_klass.map { |klass, ids| klass.where(id: ids) }.flatten
end