Module: Ohm::Geoindex::ClassMethods

Defined in:
lib/ohm/geoindex.rb

Instance Method Summary collapse

Instance Method Details

#geoindex(coords) ⇒ Object



33
34
35
# File 'lib/ohm/geoindex.rb', line 33

def geoindex(coords)
  @geoindex = coords
end

#within(center, radius, withdist: nil, sort: nil) ⇒ Object

Raises:

  • (IndexNotFound)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ohm/geoindex.rb', line 37

def within(center, radius, withdist: nil, sort: nil)
  raise IndexNotFound unless @geoindex
  args = center.is_a?(self.ancestors.first) ? ['GEORADIUSBYMEMBER', key[:geoindex], center.id] : ['GEORADIUS', key[:geoindex], *center]
  args << parse_radius(radius)
  args << sort if sort
  args << 'withdist' if withdist
  results = redis.call(*args.flatten)

  # extract ids so we can fetch all at once
  # can be [:id, :id, ...] or [[:id, :dist], [:id, :dist], ...]
  ids = results.map { |r| [*r][0] }
  models = self.ancestors.first.fetch(ids)

  if withdist
    results.each_with_index.map { |r,i| [models[i], r[1].to_f] }
  else
    models
  end
end