sequel-location 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ module Sequel
2
+ module Postgres
3
+ class PgLocation
4
+ module DatabaseMethods
5
+ def add_extension(name)
6
+ quoted_name = quote_identifier(name) if name
7
+ run("CREATE EXTENSION IF NOT EXISTS #{quoted_name}")
8
+ end
9
+
10
+ def drop_extension(name)
11
+ quoted_name = quote_identifier(name) if name
12
+ run("DROP EXTENSION IF EXISTS #{quoted_name}")
13
+ end
14
+
15
+ def add_location_trigger(table, op={})
16
+ self.run("CREATE FUNCTION update_#{table.to_s}_ll_point() RETURNS TRIGGER AS 'BEGIN NEW.#{op[:earth_point] || "ll_point"}=ll_to_earth(NEW.#{op[:latitude] || "latitude"}, NEW.#{op[:longitude] || "longitude"}); return NEW; END;' LANGUAGE plpgsql;")
17
+ self.run("CREATE TRIGGER trigger_#{table.to_s}_ll_point BEFORE INSERT OR UPDATE ON #{table.to_s} FOR ROW EXECUTE PROCEDURE update_#{table.to_s}_ll_point();")
18
+ end
19
+
20
+ def drop_location_trigger(table)
21
+ self.run("DROP TRIGGER trigger_#{table.to_s}_ll_point ON #{table.to_s};")
22
+ self.run("DROP FUNCTION update_#{table.to_s}_ll_point();")
23
+ end
24
+ end
25
+ end
26
+ end
27
+ Database.register_extension(:pg_location, Postgres::PgLocation::DatabaseMethods)
28
+ end
29
+
30
+
@@ -0,0 +1,33 @@
1
+ module Sequel
2
+ module Plugins
3
+ module PgLocation
4
+
5
+ def self.configure(model, opts={})
6
+ model.instance_eval do
7
+ @location_cache_field = opts[:earth_point] ||= :ll_point
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ attr_reader :location_cache_field
13
+
14
+ def inherited(subclass)
15
+ super
16
+ subclass.instance_variable_set(:@location_cache_field, instance_variable_get(:@location_cache_field))
17
+ end
18
+ end
19
+
20
+ module DatasetMethods
21
+ def nearest(lat,lng,radius)
22
+ location_cache_field = model.location_cache_field
23
+ radius_in_km = (radius.to_i * 1609.3).to_f
24
+ lat = lat.to_f
25
+ lng = lng.to_f
26
+ where("earth_box(ll_to_earth(?,?),?) @> #{location_cache_field}", lat, lng, radius_in_km).where("earth_distance(ll_to_earth(?, ?), #{location_cache_field}) < ?", lat, lng, radius_in_km).select_append{
27
+ (Sequel.function(:earth_distance, Sequel.function(:ll_to_earth,lat,lng), location_cache_field)).as(distance)
28
+ }.order(:distance)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-location
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -17,7 +17,8 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/sequel-location.rb
20
+ - lib/sequel/extensions/pg_location.rb
21
+ - lib/sequel/plugins/pg_location.rb
21
22
  homepage: https://github.com/nickgartmann/sequel-location
22
23
  licenses: []
23
24
  post_install_message:
@@ -1,78 +0,0 @@
1
- module Sequel
2
- module Plugins
3
- module Location
4
-
5
- def self.configure(model, opts={})
6
- model.instance_eval do
7
- @location_cache_field = opts[:earth_point] ||= :ll_point
8
- end
9
- end
10
-
11
- module ClassMethods
12
- attr_reader :location_cache_field
13
-
14
- def inherited(subclass)
15
- super
16
- subclass.instance_variable_set(:@location_cache_field, instance_variable_get(:@location_cache_field))
17
- end
18
- end
19
-
20
- module DatasetMethods
21
- def nearest(lat,lng,radius)
22
- location_cache_field = model.location_cache_field
23
- radius_in_km = (radius.to_i * 1609.3).to_f
24
- lat = lat.to_f
25
- lng = lng.to_f
26
- where("earth_box(ll_to_earth(?,?),?) @> #{location_cache_field}", lat, lng, radius_in_km).where("earth_distance(ll_to_earth(?, ?), #{location_cache_field}) < ?", lat, lng, radius_in_km).select_append{
27
- (Sequel.function(:earth_distance, Sequel.function(:ll_to_earth,lat,lng), location_cache_field)).as(distance)
28
- }.order(:distance)
29
- end
30
- end
31
- end
32
- end
33
- end
34
-
35
- module Sequel
36
- module Postgres
37
- class AlterTableGenerator < Sequel::Schema::AlterTableGenerator
38
- def add_location_trigger(options={})
39
- @operations << {:op=>:create_location_function}.merge(options)
40
- @operations << {:op=>:create_location_trigger}
41
- end
42
-
43
- def drop_location_trigger(options={})
44
- @operations << {:op=>:drop_location_trigger}
45
- @operations << {:op=>:drop_location_function}
46
- end
47
- end
48
-
49
- class Database < Sequel::Database
50
- def add_extension(name)
51
- quoted_name = quote_identifier(name) if name
52
- run("CREATE EXTENSION IF NOT EXISTS #{quoted_name}")
53
- end
54
-
55
- def drop_extension(name)
56
- quoted_name = quote_identifier(name) if name
57
- run("DROP EXTENSION IF EXISTS #{quoted_name}")
58
- end
59
-
60
- def alter_table_sql(table, op)
61
- case op[:op]
62
- when :create_location_function
63
- self.run("CREATE FUNCTION update_#{table.to_s}_ll_point() RETURNS TRIGGER AS 'BEGIN NEW.#{op[:earth_point] || "ll_point"}=ll_to_earth(NEW.#{op[:latitude] || "latitude"}, NEW.#{op[:longitude] || "longitude"}); return NEW; END;' LANGUAGE plpgsql;")
64
- when :create_location_trigger
65
- self.run("CREATE TRIGGER trigger_#{table.to_s}_ll_point BEFORE INSERT OR UPDATE ON #{table.to_s} FOR ROW EXECUTE PROCEDURE update_#{table.to_s}_ll_point();")
66
- when :drop_location_trigger
67
- self.run("DROP TRIGGER trigger_#{table.to_s}_ll_point ON #{table.to_s};")
68
- when :drop_location_function
69
- self.run("DROP FUNCTION update_#{table.to_s}_ll_point();")
70
- else
71
- super(table,op)
72
- end
73
- end
74
- end
75
- end
76
- end
77
-
78
-