active_record_mysql_spatial 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1fb8466cfa0fa405fd35472dfa05122712db0dc27b7a3daad65d6c0329736ab
4
- data.tar.gz: c58c26a2abff58ae9d9c32e710b7898db5648a5980db891deb71911b82336140
3
+ metadata.gz: 52dc505e977c105df50ab13b4c4bf1363d778ac41479b0ff578632c4d627a7a4
4
+ data.tar.gz: de9e452572739eeb2634e8ef919ba4683038b05f3fb9ad47bcd1435c6d036c0d
5
5
  SHA512:
6
- metadata.gz: ded6ba2548871b914758b5306c8b0b6b68f3922e059c3895e2e938e7a7fe6cb781f42e084d46b9871ba71e7a429097f2b9ed52d66b5e415f1bd2132b9d17b7c1
7
- data.tar.gz: 162ab35dd4901102eb470bf71c7086ea1febd6de8d24695415a4be983df2b51cf1bb0399b0dac7e9fa1b0aca32e7cbea945fd588ffbcfd14e2f1de27b14876e0
6
+ metadata.gz: e3888cef99591d42373885f9f7c06913f0226a2df5eb5f2949f102565f2660a7a76b14896f6148aa881de5b4640ab6bd8d8842247d7e91f34b4cfc63cb81aca4
7
+ data.tar.gz: 8320501949446a2e6a316b4fac74d1bf026db27e23ec8c65808e5fd3e4766119205fc69c5755a516c04a1111d3f5b15f5e805f2d8b01642a8d0e60daac7e04c6
data/README.md CHANGED
@@ -1,35 +1,102 @@
1
- # ActiveRecordMysqlSpatial
1
+ ActiveRecord extension for MySQL Spatial Data.
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ # Installation
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/active_record_mysql_spatial`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ ```sh
6
+ gem 'active_record_mysql_spatial'
7
+ ```
6
8
 
7
- ## Installation
9
+ # Migration
8
10
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
11
+ This extension provides some data type methods for ActiveRecord migration and also register those types to the schema.
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ ```rb
14
+ # frozen_string_literal: true
12
15
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+ class CreatePositions < ActiveRecord::Migration[7.2]
17
+ def change
18
+ create_table :positions do |t|
19
+ t.linestring :ls
20
+ t.multilinestring :mls
21
+ t.point :pt
14
22
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
23
+ t.column :tls, :linestring
24
+ t.column :tmls, :multilinestring
25
+ t.column :tpt, :point
16
26
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
27
+ t.timestamps
28
+ end
29
+ end
30
+ end
31
+ ```
18
32
 
19
- ## Usage
33
+ Without this extension, even you use `t.column` to define the column for the table, you will receive the error message in the `schema.rb`
20
34
 
21
- TODO: Write usage instructions here
35
+ ```rb
36
+ # Could not dump table "positions" because of following StandardError
37
+ # Unknown type 'linestring' for column 'tls'
38
+ ```
22
39
 
23
- ## Development
40
+ # Usage
24
41
 
25
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+ ## Point
26
43
 
27
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+ ```rb
45
+ position = Position.create!(pt: { x: 1, y: 2 })
28
46
 
29
- ## Contributing
47
+ position = Position.create!(pt: [1, 2])
30
48
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/active_record_mysql_spatial.
49
+ p position.pt.x # puts x
50
+ p position.pt.y # puts y
51
+ ```
32
52
 
33
- ## License
53
+ ## Linestring
34
54
 
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
55
+ ```rb
56
+ position = Position.create!(ls: [[1, 2], [2, 3]])
57
+
58
+ p position.ls.coordinates # puts all points
59
+ p position.ls.coordinates.first.x # puts x of first point
60
+ p position.ls.coordinates.last.y # puts y of last point
61
+ ```
62
+
63
+ ## Multilinestring
64
+
65
+ ```rb
66
+ position = Position.create!(mls: [[[1, 2], [2, 3]]])
67
+
68
+ p position.mls.items # puts all linestrings
69
+ p position.mls.items.first.coordinates.first.x # puts x of first point of first linestring
70
+ p position.mls.items.last.coordinates.last.y # puts y of last point of last linestring
71
+ ```
72
+
73
+ # Custom result class
74
+
75
+ In your business, you may need to use spatial data for a purpose. To load data and map to the semantic data for your business, create a class and override the method `cast_value`.
76
+
77
+ ```rb
78
+ class YourClass < ActiveRecordMysqlSpatial::ActiveRecord::MySQL::Linestring
79
+ attr_reader :sum_x, :sum_y
80
+
81
+ private
82
+
83
+ def cast_value(value)
84
+ super
85
+
86
+ @sum_x, @sum_y = @coordinates.reduce([0, 0]) do |sum, point|
87
+ sum[0] += point.x.to_i
88
+ sum[1] += point.y.to_i
89
+ sum
90
+ end
91
+
92
+ self
93
+ end
94
+ end
95
+
96
+ # models/position.rb
97
+ class Position < ApplicationRecord
98
+ include ActiveRecordMysqlSpatial::ActsAsSpatial
99
+
100
+ acts_as_linestring :ls, serializer: YourClass
101
+ end
102
+ ```
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module ActiveRecordMysqlSpatial
6
+ module ActsAsSpatial
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def acts_as_linestring(*columns, serializer: nil)
11
+ return if serializer.blank?
12
+
13
+ columns.each do |col|
14
+ attribute(col, serializer.new)
15
+ end
16
+ end
17
+
18
+ alias_method :acts_as_point, :acts_as_linestring
19
+ alias_method :acts_as_multilinestring, :acts_as_linestring
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordMysqlSpatial
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -9,6 +9,7 @@ require_relative 'active_record_mysql_spatial/active_record/mysql/point'
9
9
  require_relative 'active_record_mysql_spatial/active_record/native_types'
10
10
  require_relative 'active_record_mysql_spatial/active_record/quoting'
11
11
  require_relative 'active_record_mysql_spatial/active_record/register_types'
12
+ require_relative 'active_record_mysql_spatial/acts_as_spatial'
12
13
  require_relative 'active_record_mysql_spatial/geometry'
13
14
  require_relative 'active_record_mysql_spatial/version'
14
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_mysql_spatial
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alpha
@@ -49,6 +49,7 @@ files:
49
49
  - lib/active_record_mysql_spatial/active_record/native_types.rb
50
50
  - lib/active_record_mysql_spatial/active_record/quoting.rb
51
51
  - lib/active_record_mysql_spatial/active_record/register_types.rb
52
+ - lib/active_record_mysql_spatial/acts_as_spatial.rb
52
53
  - lib/active_record_mysql_spatial/geometry.rb
53
54
  - lib/active_record_mysql_spatial/version.rb
54
55
  homepage: https://github.com/zgid123/active_record_mysql_spatial