geodistance_arel 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d8a6f335aad80ebab6a9ed99df24db74466af43a
4
+ data.tar.gz: 9c6cd39d6794c872570c13c3d83280c3452f764f
5
+ SHA512:
6
+ metadata.gz: 335ca07a8b274e0df4cdbb64e8af554c9e10a7b9eab8406848b1e649ceac015926a3d3262b6629471aead4cc353cddcd7a75c7f8dbc4627999a52a669cd99153
7
+ data.tar.gz: 618ffe2b8cff6897777f37dafc6c9c3f9bd3e1e055247b815021fe09c5bcf4b5f3e712e2988cd33ace56a26c137e6de77bba232b10aeb632b53b3ed1ef7bb05f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+
4
+ Documentation:
5
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.13.7
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rubens.bueno@vagas.com.br. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Rubens Minoru Andako Bueno
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # GeoDistanceArel
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'geodistance_arel'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install geodistance_arel
18
+
19
+ ## Development
20
+
21
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+ 110.10.1.193
23
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubensmabueno/geodistance_arel. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
28
+
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
33
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.pattern = FileList['spec/**/*_spec.rb']
8
+ task.verbose = false
9
+ end
10
+
11
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'haversine/activerecord'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'geodistance_arel'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'geodistance_arel'
8
+ spec.version = GeoDistanceArel::VERSION
9
+ spec.authors = ['Rubens Minoru Andako Bueno', 'Bruno Da Silva Assis']
10
+ spec.email = ['rubensmabueno@hotmail.com', 'brunoassis@gmail.com']
11
+
12
+ spec.summary =
13
+ 'A simple implementation of a Arel field to calculate GeoDistances.'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'arel', '>= 7.0'
24
+ spec.add_dependency 'activesupport', '>= 5.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.13'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.5'
29
+ end
@@ -0,0 +1,5 @@
1
+ require 'geodistance_arel/glue'
2
+
3
+ module GeoDistanceArel
4
+ VERSION = '0.1.0'.freeze
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'geodistance_arel/adapters/postgresql'
2
+
3
+ module GeoDistanceArel
4
+ module Adapters
5
+ end
6
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ require 'arel'
3
+ require 'active_support/core_ext/array/grouping'
4
+
5
+ module GeoDistanceArel
6
+ module Adapters
7
+ module PostgreSQL # nodoc
8
+ def literal_of(attr)
9
+ Arel::Nodes::SqlLiteral.new(attr.to_s)
10
+ end
11
+
12
+ def radians_of(attr)
13
+ Arel::Nodes::NamedFunction.new('RADIANS', [attr])
14
+ end
15
+
16
+ def cos_of(attr)
17
+ Arel::Nodes::NamedFunction.new('COS', [attr])
18
+ end
19
+
20
+ def sin_of(attr)
21
+ Arel::Nodes::NamedFunction.new('SIN', [attr])
22
+ end
23
+
24
+ def acos_of(attr)
25
+ Arel::Nodes::NamedFunction.new('ACOS', [attr])
26
+ end
27
+
28
+ def least_of(attr)
29
+ Arel::Nodes::NamedFunction.new('least', [1, attr])
30
+ end
31
+
32
+ def multiply(*attrs)
33
+ Arel::Nodes::Multiplication.new(
34
+ *attrs.in_groups(2, false).map do |group|
35
+ if group.size > 1
36
+ multiply(*group)
37
+ else
38
+ group
39
+ end
40
+ end.flatten
41
+ )
42
+ end
43
+
44
+ def addition(*attrs)
45
+ Arel::Nodes::Addition.new(
46
+ *attrs.in_groups(2, false).map do |group|
47
+ if group.size > 1
48
+ addition(*group)
49
+ else
50
+ group
51
+ end
52
+ end.flatten
53
+ )
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,6 @@
1
+ require 'geodistance_arel/formulas/haversine'
2
+
3
+ module GeoDistanceArel
4
+ module Formulas
5
+ end
6
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GeoDistanceArel
4
+ module Formulas
5
+ class Haversine
6
+ def initialize(model_class, lat_from_field = :lat, lng_from_field = :lng)
7
+ @model_class = model_class
8
+ @lat_from_field = lat_from_field
9
+ @lng_from_field = lng_from_field
10
+ end
11
+
12
+ def distance_field_in(lat_to, lng_to, multiplier = 6371)
13
+ multiply(
14
+ distance_formula(
15
+ lat_to,
16
+ lng_to,
17
+ @model_class.arel_table[@lat_from_field],
18
+ @model_class.arel_table[@lng_from_field]
19
+ ),
20
+ multiplier
21
+ )
22
+ end
23
+
24
+ def distance_formula(lat_to, lng_to, lat_from, lng_from)
25
+ acos_of(
26
+ least_of(
27
+ distance_tetha_formula(
28
+ radians_of(literal_of(lat_to)),
29
+ radians_of(literal_of(lng_to)),
30
+ radians_of(lat_from),
31
+ radians_of(lng_from)
32
+ )
33
+ )
34
+ )
35
+ end
36
+
37
+ def distance_tetha_formula(lat_to, lng_to, lat_from, lng_from)
38
+ addition(
39
+ multiply(
40
+ cos_of(lng_to), cos_of(lat_to), cos_of(lng_from), cos_of(lat_from)
41
+ ),
42
+ multiply(
43
+ cos_of(lat_to), sin_of(lng_to), cos_of(lat_from), sin_of(lng_from)
44
+ ),
45
+ multiply(sin_of(lat_to), sin_of(lat_from))
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ require 'geodistance_arel/formulas'
3
+ require 'geodistance_arel/adapters'
4
+
5
+ module GeoDistanceArel
6
+ module Glue
7
+ attr_accessor :lat_column_name, :lng_column_name, :formula
8
+
9
+ OPTION_SYMBOLS = [:lat_column_name, :lng_column_name].freeze
10
+
11
+ def distance_field(lat_to, lng_to)
12
+ formula.distance_field_in(lat_to, lng_to)
13
+ end
14
+
15
+ def act_as_distanceable(options = {})
16
+ self.lat_column_name = options[:lat_column_name] || 'lat'
17
+ self.lng_column_name = options[:lng_column_name] || 'lng'
18
+ self.formula = GeoDistanceArel::Formulas::Haversine
19
+ .include(GeoDistanceArel::Adapters::PostgreSQL)
20
+ .new(self, lat_column_name, lng_column_name)
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geodistance_arel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rubens Minoru Andako Bueno
8
+ - Bruno Da Silva Assis
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-02-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: arel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '7.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '7.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activesupport
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '5.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '5.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.13'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.13'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.5'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.5'
84
+ description:
85
+ email:
86
+ - rubensmabueno@hotmail.com
87
+ - brunoassis@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".rubocop.yml"
95
+ - ".travis.yml"
96
+ - CODE_OF_CONDUCT.md
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - bin/console
102
+ - bin/setup
103
+ - geodistance-arel.gemspec
104
+ - lib/geodistance_arel.rb
105
+ - lib/geodistance_arel/adapters.rb
106
+ - lib/geodistance_arel/adapters/postgresql.rb
107
+ - lib/geodistance_arel/formulas.rb
108
+ - lib/geodistance_arel/formulas/haversine.rb
109
+ - lib/geodistance_arel/glue.rb
110
+ homepage:
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.8
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: A simple implementation of a Arel field to calculate GeoDistances.
134
+ test_files: []