armg 0.3.0 → 0.4.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
  SHA1:
3
- metadata.gz: 84e6b3ee252fb5c11271a5ef0ae7ab318d4a509d
4
- data.tar.gz: 2c2be3dad6a3db05718938e80b238debc0c13168
3
+ metadata.gz: 469ae0fd83f0f67d9e31b66f79c25f2a8649ab6a
4
+ data.tar.gz: fe09fd8a362b915e18be5462b76f17c4b74deab3
5
5
  SHA512:
6
- metadata.gz: 4d9aa995ba5b6b5a228ab6f6eac7c2640a2e9db5a37de2e71e759c3b6104026277d18e688fbf451853a9258257be461470719e4be8049b5fcda7e3ee978d2b6c
7
- data.tar.gz: 1f609a7196e10b8c29e71399988f45389443df8960084347cce53e3992b2920ced2b8051b034776c5391907091006272aade83b9f88efb5f966ed393172c66e2
6
+ metadata.gz: eb3abcf2d2a06ffff03a1a94fee61efc2f2acee1e2fb2c4aebf2bf12811447920bf20e5b25472b212258c482708de2de246f4e943f01c28af816e8fb3c7fb6f1
7
+ data.tar.gz: 2c23cea430e9e830a64ac1cb66b15421ba15e4e44a595af089ff3ff8a4f32e5c2e40fb8da1e64ddb38e5826bd747419539ef2540a015a3f48b9576be80628770
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Add MySQL geometry type to Active Record.
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/armg.svg)](https://badge.fury.io/rb/armg)
5
6
  [![Build Status](https://travis-ci.org/winebarrel/armg.svg?branch=master)](https://travis-ci.org/winebarrel/armg)
6
7
 
7
8
  ## Installation
@@ -26,7 +27,7 @@ Or install it yourself as:
26
27
  require 'active_record'
27
28
  require 'armg'
28
29
 
29
- ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'my_db');
30
+ ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'my_db')
30
31
 
31
32
  ActiveRecord::Migration.create_table :geoms, options: 'ENGINE=MyISAM' do |t|
32
33
  t.geometry 'location', null: false
@@ -42,3 +43,48 @@ Geom.create!(location: point)
42
43
  Geom.first
43
44
  #=> #<Geom id: 1, location: #<RGeo::Cartesian::PointImpl:0x... "POINT (-122.1 47.3)">>
44
45
  ```
46
+
47
+ ## Using custom WKB parser
48
+
49
+ ```ruby
50
+ class CustomParser
51
+ def initialize
52
+ factory = RGeo::Geographic.spherical_factory(srid: 0)
53
+ @parser = RGeo::WKRep::WKBParser.new(factory, support_ewkb: true)
54
+ end
55
+
56
+ def parse(wkb)
57
+ wkb_without_srid = wkb.bytes[4..-1].pack('c*')
58
+ @parser.parse(wkb_without_srid)
59
+ end
60
+ end
61
+
62
+ Armg.wkb_parser = CustomParser.new
63
+
64
+ Geom.take
65
+ #=> #<Geom id: 1, location: #<RGeo::Geographic::SphericalPointImpl:0x... "POINT (-122.1 47.3)">>
66
+ ```
67
+
68
+ ## Using custom WKB generator
69
+
70
+ ```ruby
71
+ class CustomGenerator
72
+ def initialize
73
+ @wkt_parser = RGeo::WKRep::WKTParser.new(nil, support_ewkt: true)
74
+ @generator = RGeo::WKRep::WKBGenerator.new(type_format: :ewkb, little_endian: true)
75
+ end
76
+
77
+ def generate(value)
78
+ if value.is_a?(String)
79
+ value = @wkt_parser.parse(value)
80
+ end
81
+
82
+ srid = "\x00\x00\x00\x00"
83
+ srid + @generator.generate(value)
84
+ end
85
+ end
86
+
87
+ Armg.wkb_generator = CustomGenerator.new
88
+
89
+ Geom.create!(id: 4, location: 'Point(-122.1 47.3)')
90
+ ```
@@ -4,6 +4,8 @@ require 'armg/version'
4
4
 
5
5
  ActiveSupport.on_load(:active_record) do
6
6
  require 'active_record/connection_adapters/abstract_mysql_adapter'
7
+ require 'armg/wkb_generator'
8
+ require 'armg/wkb_parser'
7
9
  require 'armg/abstract_mysql_adapter_ext'
8
10
  require 'armg/mysql_geometry'
9
11
  require 'armg/table_definition_ext'
@@ -1,22 +1,22 @@
1
1
  module Armg
2
- @wkb_parser_factory = Armg::MysqlGeometry::DEFAULT_WKB_PARSER_FACTORY
3
- @wkb_generator_factory = Armg::MysqlGeometry::DEFAULT_WKB_GENERATOR_FACTORY
2
+ @wkb_parser = Armg::MysqlGeometry::DEFAULT_WKB_PARSER
3
+ @wkb_generator = Armg::MysqlGeometry::DEFAULT_WKB_GENERATOR
4
4
 
5
5
  class << self
6
- def wkb_parser_factory
7
- @wkb_parser_factory
6
+ def wkb_parser
7
+ @wkb_parser
8
8
  end
9
9
 
10
- def wkb_parser_factory=(factory)
11
- @wkb_parser_factory = factory
10
+ def wkb_parser=(parser)
11
+ @wkb_parser = parser
12
12
  end
13
13
 
14
- def wkb_generator_factory
15
- @wkb_generator_factory
14
+ def wkb_generator
15
+ @wkb_generator
16
16
  end
17
17
 
18
- def wkb_generator_factory=(factory)
19
- @wkb_generator_factory = factory
18
+ def wkb_generator=(generator)
19
+ @wkb_generator = generator
20
20
  end
21
21
  end
22
22
  end
@@ -1,18 +1,8 @@
1
1
  class Armg::MysqlGeometry < ActiveModel::Type::Value
2
2
  include ActiveModel::Type::Helpers::Mutable
3
3
 
4
- DEFAULT_WKB_PARSER_FACTORY = proc do |wkb|
5
- srid = wkb[0..3].unpack('L<').first
6
- [ RGeo::WKRep::WKBParser.new(nil, support_ewkb: true, default_srid: srid),
7
- wkb[4..-1],
8
- ]
9
- end
10
-
11
- DEFAULT_WKB_GENERATOR_FACTORY = proc do |value|
12
- [ RGeo::WKRep::WKBGenerator.new(type_format: :ewkb, little_endian: true),
13
- [value.srid].pack('L<'),
14
- ]
15
- end
4
+ DEFAULT_WKB_PARSER = Armg::WkbParser.new
5
+ DEFAULT_WKB_GENERATOR = Armg::WkbGenerator.new
16
6
 
17
7
  def type
18
8
  :geometry
@@ -20,8 +10,7 @@ class Armg::MysqlGeometry < ActiveModel::Type::Value
20
10
 
21
11
  def deserialize(value)
22
12
  if value.is_a?(::String)
23
- wkb_parser, value = Armg.wkb_parser_factory.call(value)
24
- wkb_parser.parse(value)
13
+ Armg.wkb_parser.parse(value)
25
14
  else
26
15
  value
27
16
  end
@@ -31,9 +20,7 @@ class Armg::MysqlGeometry < ActiveModel::Type::Value
31
20
  if value.nil?
32
21
  nil
33
22
  else
34
- wkb_generator, srid = Armg.wkb_generator_factory.call(value)
35
- wkb = wkb_generator.generate(value)
36
- srid + wkb
23
+ Armg.wkb_generator.generate(value)
37
24
  end
38
25
  end
39
26
  end
@@ -1,3 +1,3 @@
1
1
  module Armg
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -0,0 +1,16 @@
1
+ class Armg::WkbGenerator
2
+ DEFAULT_OPTIONS = {
3
+ type_format: :ewkb,
4
+ little_endian: true,
5
+ }
6
+
7
+ def initialize(factory = nil, options = {})
8
+ options = DEFAULT_OPTIONS.merge(options)
9
+ @generator = RGeo::WKRep::WKBGenerator.new(options)
10
+ end
11
+
12
+ def generate(deserialized_value)
13
+ srid = [deserialized_value.srid].pack('L<')
14
+ srid + @generator.generate(deserialized_value)
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ class Armg::WkbParser
2
+ DEFAULT_OPTIONS = {
3
+ support_ewkb: true,
4
+ }
5
+
6
+ def initialize(factory = nil, options = {})
7
+ options = DEFAULT_OPTIONS.merge(options)
8
+ @parser = RGeo::WKRep::WKBParser.new(factory, options)
9
+ end
10
+
11
+ def parse(wkb)
12
+ wkb = wkb.bytes
13
+ srid = wkb.slice!(0..3)
14
+ wkb[4] = 0x20
15
+ wkb.insert(5, *srid)
16
+ @parser.parse(wkb.pack('c*'))
17
+ end
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: armg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2017-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -132,6 +132,8 @@ files:
132
132
  - lib/armg/mysql_geometry.rb
133
133
  - lib/armg/table_definition_ext.rb
134
134
  - lib/armg/version.rb
135
+ - lib/armg/wkb_generator.rb
136
+ - lib/armg/wkb_parser.rb
135
137
  homepage: https://github.com/winebarrel/armg
136
138
  licenses:
137
139
  - MIT
@@ -152,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
154
  version: '0'
153
155
  requirements: []
154
156
  rubyforge_project:
155
- rubygems_version: 2.6.12
157
+ rubygems_version: 2.5.2
156
158
  signing_key:
157
159
  specification_version: 4
158
160
  summary: Add MySQL geometry type to Active Record.