mysql_point 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc2612e36becbac4abe561f588e43c3772f19428
4
+ data.tar.gz: 86a04677384baeabdca0ea45168f550b0ca85a3e
5
+ SHA512:
6
+ metadata.gz: a0d08140c94000a0506b7c113e11af236baa128dc1d1f41786464d039bc7a5ba6caf7f0dd850fc7741765eec40ce1f388522553fcbb1f953d7010ca78a1153c1
7
+ data.tar.gz: 647e13ac125c70d452b4313fbadb0efa898e2001ed1ffd73a73a6d061bd366c88f5a973cb0c7434d24212c3ce46cced363b92923c09062e95c287ddb4b1130ae
@@ -0,0 +1,16 @@
1
+ version: 2
2
+
3
+ jobs:
4
+ build:
5
+ working_directory: ~/mysql_point
6
+ docker:
7
+ - image: circleci/ruby:2.3
8
+ environment:
9
+ DATABASE_URL: mysql2://root@127.0.0.1/circle_test
10
+ - image: circleci/mysql:5.7
11
+
12
+ steps:
13
+ - checkout
14
+ - run: bundle install
15
+ - run: echo $DATABASE_URL
16
+ - run: appraisal rake test
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ gemfiles/*.gemfile.lock
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.1-lance
@@ -0,0 +1,7 @@
1
+ appraise 'activerecord-5.0' do
2
+ gem 'activerecord', '~> 5.0'
3
+ end
4
+
5
+ appraise 'activerecord-5.1' do
6
+ gem 'activerecord', '~> 5.1'
7
+ end
@@ -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 lance@cainlevy.net. 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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql_point.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Lance Ivy
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.
@@ -0,0 +1,87 @@
1
+ # MySQLPoint
2
+
3
+ Provides basic ActiveRecord support for MySQL `point` fields. This becomes useful in MySQL 5.7.6, which adds [`ST_DISTANCE_SPHERE`](https://dev.mysql.com/doc/refman/5.7/en/spatial-convenience-functions.html#function_st-distance-sphere) for practical distance-between-points-on-Earth calculations.
4
+
5
+ ## Features
6
+
7
+ Included:
8
+
9
+ * migrations
10
+ * schema dumping and loading
11
+ * reading and writing from database
12
+ * a Ruby struct
13
+ * ActiveModel validator for Earth coordinates
14
+
15
+ Future scope:
16
+
17
+ * ARel helper to find records near a static location
18
+
19
+ Never in scope:
20
+
21
+ * full `geometry` field support in MySQL
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'mysql_point'
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ **Migrating**
34
+
35
+ ```ruby
36
+ class MyMigration < ActiveRecord::Migration
37
+ def change
38
+ create_table :public_water_fountains do |t|
39
+ t.point 'location', null: false
40
+ t.timestamps
41
+ end
42
+ end
43
+ end
44
+ ```
45
+
46
+ **Validating**
47
+
48
+ ```ruby
49
+ class PublicWaterFountain
50
+ validates :location, coordinate: true
51
+ end
52
+ ```
53
+
54
+ **Setting**
55
+
56
+ ```ruby
57
+ # scalar values can be passed in WKT format, with longitude in first position
58
+ PublicWaterFountain.new(location: 'POINT(-73.9635288 40.7812022)')
59
+
60
+ # structured values can be passed as a MySQLPoint::Coordinate
61
+ PublicWaterFountain.new(
62
+ location: MySQLPoint::Coordinate(longitude: -73.9635288, latitude: 40.7812022)
63
+ )
64
+ ```
65
+
66
+ **Querying**
67
+
68
+ ```sql
69
+ -- all public water fountains within 5 miles of Central Park
70
+ SELECT *
71
+ FROM public_water_fountains
72
+ WHERE ST_DISTANCE_SPHERE(location, POINT(-73.9675438, 40.7828687)) * 0.000621371 < 5
73
+ ```
74
+
75
+ ## Development
76
+
77
+ 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.
78
+
79
+ 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).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/empaticoorg/mysql_point. 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.
84
+
85
+ ## Code of Conduct
86
+
87
+ Everyone interacting in the MySQLPoint project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/empaticoorg/mysql_point/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mysql_point'
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(__FILE__)
@@ -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,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.1"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,10 @@
1
+ class CoordinateValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ return if value.respond_to?(:latitude) &&
4
+ (-90..90).cover?(value.latitude) &&
5
+ value.respond_to?(:longitude) &&
6
+ (-180..180).cover?(value.longitude)
7
+
8
+ record.errors.add(attribute, options[:message] || :invalid)
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_record'
2
+
3
+ module MySQLPoint
4
+ end
5
+
6
+ require_relative 'mysql_point/version'
7
+ require_relative 'mysql_point/coordinate'
8
+ require_relative 'coordinate_validator'
9
+
10
+ # enables column type in ActiveRecord
11
+ require_relative 'mysql_point/type'
12
+ ActiveRecord::Base.connection.type_map.register_type(/^point/, MySQLPoint::Type.new)
13
+
14
+ # enables a Coordinate to be sent as WKT and converted by a MySQL function on insert/update
15
+ require_relative 'mysql_point/quoting'
16
+ ActiveRecord::Base.connection.class.prepend MySQLPoint::Quoting
17
+
18
+ # enables shorthand syntax in migrations and db/schema.rb
19
+ require_relative 'mysql_point/column_method'
20
+ ActiveRecord::ConnectionAdapters::MySQL::TableDefinition.include MySQLPoint::ColumnMethod
21
+ ActiveRecord::ConnectionAdapters::MySQL::Table.include MySQLPoint::ColumnMethod
22
+
23
+ # enables schema dumping into migration syntax
24
+ ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::NATIVE_DATABASE_TYPES[:point] = {
25
+ name: 'point'
26
+ }
@@ -0,0 +1,5 @@
1
+ module MySQLPoint::ColumnMethod
2
+ def point(*args, **options)
3
+ args.each{|name| column(name, :point, options) }
4
+ end
5
+ end
@@ -0,0 +1,46 @@
1
+ # A struct for latitude/longitude coordinates, with serializers and deserializers for the standard
2
+ # WKT and WKB formats.
3
+ #
4
+ # # WKT
5
+ #
6
+ # `POINT(<longitude> <latitude>)`
7
+ #
8
+ # # WKB
9
+ #
10
+ # * byte 0: byte order (little endian is 01)
11
+ # * bytes 1-4: WKB type (point is 01)
12
+ # * bytes 5-12: X coordinate (longitude)
13
+ # * bytes 13-21: Y coordinate (latitude)
14
+ class MySQLPoint::Coordinate
15
+ # POINT(<longitude> <latitude>)
16
+ def self.from_wkt(str)
17
+ md = str.match(/\APOINT\((-?[0-9.]*) (-?[0-9.]*)\)\z/)
18
+ new(longitude: md[1], latitude: md[2]) if md
19
+ end
20
+
21
+ def self.from_wkb(binstr)
22
+ type = binstr[1, 4].unpack(binstr[0] == "\x01" ? 'V' : 'N').first
23
+ return unless type == 1
24
+
25
+ x = binstr[5, 8].unpack('D').first
26
+ y = binstr[13, 8].unpack('D').first
27
+
28
+ new(longitude: x, latitude: y)
29
+ end
30
+
31
+ attr_reader :longitude
32
+ attr_reader :latitude
33
+
34
+ def initialize(longitude:, latitude:)
35
+ @longitude = longitude.to_f if longitude
36
+ @latitude = latitude.to_f if latitude
37
+ end
38
+
39
+ def to_wkt
40
+ "POINT(#{format('%.7f', longitude)} #{format('%.7f', latitude)})"
41
+ end
42
+
43
+ def to_wkb
44
+ "\x01" + [1].pack('L') + [longitude, latitude].pack('D*')
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ module MySQLPoint::Quoting
2
+ def quote(value)
3
+ if value.respond_to?(:to_wkt)
4
+ "ST_GeomFromText(#{super(value.to_wkt)})"
5
+ else
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ class MySQLPoint::Type < ActiveModel::Type::Value
2
+ # for schema dumping
3
+ def type
4
+ :point
5
+ end
6
+
7
+ # from ruby type (Coordinate) to quotable database value (Coordinate)
8
+ # this depends on the MySQLPoint::Quoting mixin to work.
9
+ def serialize(value)
10
+ value
11
+ end
12
+
13
+ # from database value (WKB, Coordinate) to ruby value (Coordinate)
14
+ def deserialize(value)
15
+ if value.respond_to?(:to_wkt)
16
+ value
17
+ elsif value
18
+ MySQLPoint::Coordinate.from_wkb(value[4..-1])
19
+ end
20
+ end
21
+
22
+ # from attribute setter (WKT, Coordinate) to ruby value (Coordinate)
23
+ # and from user input (WKT) to ruby value (Coordinate)
24
+ def cast(value)
25
+ if value.respond_to?(:to_wkt)
26
+ value
27
+ elsif value
28
+ MySQLPoint::Coordinate.from_wkt(value)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module MySQLPoint
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_point/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mysql_point'
8
+ spec.version = MySQLPoint::VERSION
9
+ spec.authors = ['Lance Ivy']
10
+ spec.email = ['lance@cainlevy.net']
11
+
12
+ spec.summary = 'ActiveRecord support for MySQL POINT fields.'
13
+ spec.homepage = 'https://github.com/empaticoorg/mysql_point'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}){|f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{^(test|spec|features)/})
29
+ end
30
+
31
+ spec.add_dependency 'activerecord', '>= 5.0'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.15'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'minitest', '~> 5.0'
36
+ spec.add_development_dependency 'appraisal'
37
+ spec.add_development_dependency 'mysql2'
38
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_point
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Lance Ivy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - lance@cainlevy.net
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".circleci/config.yml"
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - Appraisals
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - gemfiles/activerecord_5.0.gemfile
116
+ - gemfiles/activerecord_5.1.gemfile
117
+ - lib/coordinate_validator.rb
118
+ - lib/mysql_point.rb
119
+ - lib/mysql_point/column_method.rb
120
+ - lib/mysql_point/coordinate.rb
121
+ - lib/mysql_point/quoting.rb
122
+ - lib/mysql_point/type.rb
123
+ - lib/mysql_point/version.rb
124
+ - mysql_point.gemspec
125
+ homepage: https://github.com/empaticoorg/mysql_point
126
+ licenses: []
127
+ metadata:
128
+ allowed_push_host: https://rubygems.org
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.5.1
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: ActiveRecord support for MySQL POINT fields.
149
+ test_files: []