ohm-geoindex 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0df31f7ce55dccb5db41ff451c27b638b601697
4
- data.tar.gz: da98c5300d8050312ad692499ec26d7d91d35c9d
3
+ metadata.gz: 5954ef928642867c601f0d3d0e1d250c9e83b6c5
4
+ data.tar.gz: 5cac02b2e91128831b6d986d56a2ec39d562f167
5
5
  SHA512:
6
- metadata.gz: da972d8ea43b074973b1c80e3ca08cd4acce50da15ddfe8b9c6ae5306952dfec8137cbfc972b9142e33600e58c01757bc4ca49213c487878e5eb14131fe2e76b
7
- data.tar.gz: d333e5d8359ccc5e24684516633ab1ba2ae6396dcafa35e911caf167496289b47f7e2999d992aa18d681e6fa52d6f54229d7f9a0bc8f465151ece172b8920956
6
+ metadata.gz: c2d09eb9a7869a3e7732796907f2a3019145a1bd6db28d8e63f11ed66bea80b35d1d1b20bb4c024058522862084efb6a58568b49de163b88e13ed0d5c3976276
7
+ data.tar.gz: 2215425b98bce463f19e3fd7d543d96634e6f777963d5251fbd28acd5ded4b272a92bfbc7c3c4e0b6e515deeeda5ffb4cd79238838ea1ef9933638fe160a4636
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ Gemfile.lock
4
4
  test/redis-server
5
5
  test/tmp
6
6
  vendor/*
7
+ *.gem
data/README.md CHANGED
@@ -7,16 +7,19 @@ ohm-geoindex
7
7
 
8
8
  A plugin for [Ohm](https://github.com/soveran/ohm) models which enables basic geospatial queries via the Redis [geohashing](http://redis.io/commands#geo) [API](http://cristian.regolo.cc/2015/07/07/introducing-the-geo-api-in-redis.html).
9
9
 
10
- Setup
10
+ Installation
11
11
  -----
12
12
 
13
- 1. Add the gem:
13
+ Add the gem:
14
14
 
15
15
  ```ruby
16
16
  gem 'ohm-geoindex', require: 'ohm/geoindex'
17
17
  ````
18
18
 
19
- 2. Include the module in your model and specify the attributes to be indexed. (Note that `(longitude, latitude)` is the ordering convention used throughout the geo API.)
19
+ Usage
20
+ -----
21
+
22
+ Include the module in your model and specify the attributes to be indexed. (Note that `(longitude, latitude)` is the ordering convention used throughout the geo API.)
20
23
 
21
24
  ```ruby
22
25
  class Beach < Ohm::Model
@@ -28,9 +31,6 @@ class Beach < Ohm::Model
28
31
  end
29
32
  ```
30
33
 
31
- Usage
32
- -----
33
-
34
34
  To perform a radius query, use the `within` class method.
35
35
 
36
36
  ```ruby
@@ -2,7 +2,7 @@ require 'ohm'
2
2
 
3
3
  module Ohm
4
4
  module Geoindex
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
6
6
 
7
7
  def self.included(model)
8
8
  begin
@@ -35,15 +35,19 @@ module Ohm
35
35
  @geoindex = coords
36
36
  end
37
37
 
38
- def within(center, radius, withdist: nil, sort: nil)
38
+ def within(center, radius, withdist: nil, sort: nil, count: nil)
39
39
  raise IndexNotFound unless @geoindex
40
- raise raise ArgumentError, "center must be a set of coordinates or model already in the index." unless center
40
+ unless center.is_a?(self.ancestors.first) || (center.is_a?(Array) && center.size == 2)
41
+ raise ArgumentError, "center must be a set of [lng, lat] coordinates or model already in the index."
42
+ end
41
43
 
42
44
  args = center.is_a?(self.ancestors.first) ? ['GEORADIUSBYMEMBER', key[:geoindex], center.id] : ['GEORADIUS', key[:geoindex], *center]
43
45
  args << parse_radius(radius)
46
+ args << "withdist" if withdist
44
47
  args << sort if sort
45
- args << 'withdist' if withdist
46
- results = redis.call(*args.flatten)
48
+ args << ["count", count] if count
49
+
50
+ results = redis.call(*(args.flatten))
47
51
 
48
52
  # extract ids so we can fetch all at once
49
53
  # can be [:id, :id, ...] or [[:id, :dist], [:id, :dist], ...]
@@ -38,6 +38,12 @@ class GeoindexTest < Test::Unit::TestCase
38
38
  assert_equal(@coogee, a.last)
39
39
  end
40
40
 
41
+ def test_count
42
+ a = Beach.within([151.257566, -33.921017], '10 km', sort: 'asc', count: 1) # coogee
43
+ assert_equal(1, a.size)
44
+ assert_equal(@coogee, a.first)
45
+ end
46
+
41
47
  def test_within_with_distance
42
48
  a = Beach.within(@coogee, '10 km', sort: 'asc', withdist: true)
43
49
  assert_equal(2, a.size)
@@ -46,6 +52,15 @@ class GeoindexTest < Test::Unit::TestCase
46
52
  assert_equal(2, a.size)
47
53
  end
48
54
 
55
+ def test_center_missing
56
+ assert_raise do
57
+ Beach.within(nil, '100 m', sort: 'asc')
58
+ end
59
+ assert_raise do
60
+ Beach.within([40, 40, 40], '100 m', sort: 'asc')
61
+ end
62
+ end
63
+
49
64
  def test_index_update
50
65
  a = Ohm.redis.call('ZSCORE', @bondi.class.key[:geoindex], @bondi.id)
51
66
  @bondi.update(lat: -33.921017, lng: 151.257566) # bondi moves to coogee
@@ -60,8 +75,12 @@ class GeoindexTest < Test::Unit::TestCase
60
75
  assert_equal([@coogee, @bondi], a)
61
76
  a = Beach.within(@coogee, '10000m', sort: 'asc')
62
77
  assert_equal([@coogee, @bondi], a)
78
+ a = Beach.within(@coogee, '10000 m', sort: 'asc')
79
+ assert_equal([@coogee, @bondi], a)
80
+ a = Beach.within(@coogee, '0 km', sort: 'asc')
81
+ assert_equal([@coogee], a)
63
82
  assert_raise do
64
- a = Beach.within(@coogee, 'one billion feet', 'asc')
83
+ Beach.within(@coogee, 'one billion feet', sort: 'asc')
65
84
  end
66
85
  end
67
86
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm-geoindex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eliot Shepard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-20 00:00:00.000000000 Z
11
+ date: 2016-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ohm