sequel-location 0.0.1
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.
- data/lib/sequel-location.rb +78 -0
- metadata +45 -0
@@ -0,0 +1,78 @@
|
|
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
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-location
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Gartmann
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Add support for geolocation search to sequel models
|
15
|
+
email: nick.gartmann@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/sequel-location.rb
|
21
|
+
homepage: https://github.com/nickgartmann/sequel-location
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: sequel-location
|
45
|
+
test_files: []
|