arel-haversine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_script:
5
+ - mysql -e 'create database arel_haversine_test;'
6
+ - psql -c 'create database arel_haversine_test;' -U postgres
7
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jacob Swanner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Arel::Haversine [![Build Status](https://secure.travis-ci.org/jswanner/arel-haversine.png?branch=master)](https://travis-ci.org/jswanner/arel-haversine)
2
+
3
+ Provides haversine formula, implemented with Arel.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'arel-haversine'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install arel-haversine
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ require 'arel/haversine'
23
+
24
+ class Location < ActiveRecord::Base
25
+ def self.in_order_of_proximity_to(latitude, longitude)
26
+ order(
27
+ Arel::Nodes::Haversine.new(
28
+ arel_table[:latitude],
29
+ arel_table[:longitude],
30
+ latitude,
31
+ longitude
32
+ )
33
+ )
34
+ end
35
+ end
36
+ ```
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arel/haversine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'arel-haversine'
8
+ spec.version = Arel::Haversine::VERSION
9
+ spec.authors = ['Jacob Swanner']
10
+ spec.email = ['jacob@jacobswanner.com']
11
+ spec.description = %q{Provides haversine formula, implemented with Arel; which could be added to ActiveRecord scope, for instance.}
12
+ spec.summary = %q{Provides haversine formula, implemented with Arel.}
13
+ spec.homepage = 'https://github.com/jswanner/arel-haversine'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'arel'
22
+
23
+ spec.add_development_dependency 'activerecord', '>= 3'
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'mysql2'
26
+ spec.add_development_dependency 'pg'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec', '~> 2.11'
29
+ end
@@ -0,0 +1,3 @@
1
+ require 'arel'
2
+ require 'arel/haversine/version'
3
+ require 'arel/nodes/haversine'
@@ -0,0 +1,5 @@
1
+ module Arel
2
+ module Haversine
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,89 @@
1
+ module Arel
2
+ module Nodes
3
+ class Haversine < Function
4
+ def initialize lat1, lng1, lat2, lng2, options = nil
5
+ super Multiplication.new(
6
+ Arcsine.new(
7
+ Sqrt.new(
8
+ Addition.new(
9
+ Pow.new(Sine.new(Division.new(Radians.new(lat1 - lat2), 2)), 2),
10
+ Multiplication.new(
11
+ Pow.new(Sine.new(Division.new(Radians.new(lng1 - lng2), 2)), 2),
12
+ Multiplication.new(
13
+ Cosine.new(Radians.new(lat2)),
14
+ Cosine.new(Radians.new(lat1))
15
+ )
16
+ )
17
+ )
18
+ )
19
+ ), diameter(options)
20
+ )
21
+ end
22
+
23
+ private
24
+
25
+ def diameter options
26
+ (options || {})[:unit] == :mi ? 7917.5 : 12742
27
+ end
28
+ end
29
+
30
+ %w[
31
+ Arcsine
32
+ Cosine
33
+ Radians
34
+ Sine
35
+ Sqrt
36
+ ].each do |name|
37
+ const_set name, Class.new(Unary)
38
+ end
39
+ Pow = Class.new(Binary)
40
+ end
41
+
42
+ module Visitors
43
+ module CommonMathVisitors
44
+ private
45
+
46
+ def visit_Arel_Nodes_Arcsine o
47
+ "ASIN(#{visit o.expr})"
48
+ end
49
+
50
+ def visit_Arel_Nodes_Cosine o
51
+ "COS(#{visit o.expr})"
52
+ end
53
+
54
+ def visit_Arel_Nodes_Pow o
55
+ "POW(#{visit o.left}, #{visit o.right})"
56
+ end
57
+
58
+ def visit_Arel_Nodes_Radians o
59
+ "RADIANS(#{visit o.expr})"
60
+ end
61
+
62
+ def visit_Arel_Nodes_Sine o
63
+ "SIN(#{visit o.expr})"
64
+ end
65
+
66
+ def visit_Arel_Nodes_Sqrt o
67
+ "SQRT(#{visit o.expr})"
68
+ end
69
+ end
70
+
71
+ class MySQL
72
+ include CommonMathVisitors
73
+ end
74
+
75
+ class PostgreSQL
76
+ include CommonMathVisitors
77
+ end
78
+
79
+ class ToSql
80
+ private
81
+
82
+ def visit_Arel_Nodes_Haversine o
83
+ visit(o.expressions).tap do |sql|
84
+ sql << " AS #{o.alias}" if o.alias
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,10 @@
1
+ require 'mysql2'
2
+ require 'spec_helper'
3
+
4
+ describe 'Haversine in MySQL', :mysql do
5
+ def parse_results results
6
+ results.first.first
7
+ end
8
+
9
+ include_examples 'distance calculations'
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'pg'
2
+ require 'spec_helper'
3
+
4
+ describe 'Haversine in PostgreSQL', :postgres do
5
+ def parse_results results
6
+ results.first['distance'].to_f
7
+ end
8
+
9
+ include_examples 'distance calculations'
10
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'bundler/setup'
3
+ require 'arel/haversine'
4
+ require 'active_record'
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.order = 'random'
9
+ end
10
+
11
+ Dir[File.expand_path('../support/*.rb', __FILE__)].each { |file| require file }
@@ -0,0 +1,17 @@
1
+ shared_context 'MySQL connection', :mysql do
2
+ before :all do
3
+ ActiveRecord::Base.establish_connection(
4
+ adapter: 'mysql2',
5
+ database: 'arel_haversine_test'
6
+ )
7
+ end
8
+ end
9
+
10
+ shared_context 'Postgres connection', :postgres do
11
+ before :all do
12
+ ActiveRecord::Base.establish_connection(
13
+ adapter: 'postgresql',
14
+ database: 'arel_haversine_test'
15
+ )
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ shared_examples 'distance calculations' do
2
+ let(:google) { [37.422045, -122.084347] }
3
+ let(:haversine) { Arel::Nodes::Haversine.new(*google, *san_francisco, unit: unit) }
4
+ let(:query) { project(haversine.as('distance')).to_sql }
5
+ let(:result) { parse_results(execute(query)) }
6
+ let(:san_francisco) { [37.77493, -122.419416] }
7
+
8
+ context 'default unit' do
9
+ let(:unit) { nil }
10
+
11
+ it 'returns distance between two coordinates in kilometers' do
12
+ expect(result).to be_within(0.000_001).of(49.103_006)
13
+ end
14
+ end
15
+
16
+ context 'in kilometers' do
17
+ let(:unit) { :km }
18
+
19
+ it 'returns distance between two coordinates' do
20
+ expect(result).to be_within(0.000_001).of(49.103_006)
21
+ end
22
+ end
23
+
24
+ context 'in miles' do
25
+ let(:unit) { :mi }
26
+
27
+ it 'returns distance between two coordinates' do
28
+ expect(result).to be_within(0.000_001).of(30.511_148)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ def execute query
2
+ ActiveRecord::Base.connection.execute query
3
+ end
4
+
5
+ def project clause
6
+ Arel::SelectManager.new(ActiveRecord::Base).project clause
7
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arel-haversine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jacob Swanner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: arel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: mysql2
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pg
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '2.11'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '2.11'
126
+ description: Provides haversine formula, implemented with Arel; which could be added
127
+ to ActiveRecord scope, for instance.
128
+ email:
129
+ - jacob@jacobswanner.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - .rspec
136
+ - .travis.yml
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - arel-haversine.gemspec
142
+ - lib/arel/haversine.rb
143
+ - lib/arel/haversine/version.rb
144
+ - lib/arel/nodes/haversine.rb
145
+ - spec/mysql_spec.rb
146
+ - spec/postgres_spec.rb
147
+ - spec/spec_helper.rb
148
+ - spec/support/shared_contexts.rb
149
+ - spec/support/shared_examples.rb
150
+ - spec/support/sql_helpers.rb
151
+ homepage: https://github.com/jswanner/arel-haversine
152
+ licenses:
153
+ - MIT
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 1.8.24
173
+ signing_key:
174
+ specification_version: 3
175
+ summary: Provides haversine formula, implemented with Arel.
176
+ test_files:
177
+ - spec/mysql_spec.rb
178
+ - spec/postgres_spec.rb
179
+ - spec/spec_helper.rb
180
+ - spec/support/shared_contexts.rb
181
+ - spec/support/shared_examples.rb
182
+ - spec/support/sql_helpers.rb