rgeo-activerecord 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +6 -0
- data/README.rdoc +89 -0
- data/Version +1 -0
- data/lib/rgeo/active_record.rb +66 -0
- data/lib/rgeo/active_record/adapter_test_helper.rb +147 -0
- data/lib/rgeo/active_record/ar_modifications.rb +149 -0
- data/lib/rgeo/active_record/arel_modifications.rb +73 -0
- data/lib/rgeo/active_record/common.rb +84 -0
- data/test/tc_basic.rb +90 -0
- metadata +104 -0
data/History.rdoc
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
== RGeo::ActiveRecord
|
2
|
+
|
3
|
+
RGeo::ActiveRecord is an optional {RGeo}[http://github.com/dazuma/rgeo]
|
4
|
+
module providing spatial extensions for \ActiveRecord, as well as a set
|
5
|
+
of helpers for writing spatial \ActiveRecord adapters based on \RGeo.
|
6
|
+
|
7
|
+
=== Summary
|
8
|
+
|
9
|
+
\RGeo is a key component for writing location-aware applications in the
|
10
|
+
Ruby programming language. At its core is an implementation of the
|
11
|
+
industry standard OGC Simple Features Specification, which provides data
|
12
|
+
representations of geometric objects such as points, lines, and polygons,
|
13
|
+
along with a set of geometric analysis operations. See the README for the
|
14
|
+
"rgeo" gem for more information.
|
15
|
+
|
16
|
+
RGeo::ActiveRecord is an optional \RGeo add-on module providing spatial
|
17
|
+
extensions for \ActiveRecord, as well as a set of helpers for writing
|
18
|
+
spatial \ActiveRecord adapters based on \RGeo.
|
19
|
+
|
20
|
+
=== Installation
|
21
|
+
|
22
|
+
RGeo::ActiveRecord has the following requirements:
|
23
|
+
|
24
|
+
* Ruby 1.8.7 or later. Ruby 1.9.2 or later preferred.
|
25
|
+
* \RGeo 0.2.0 or later.
|
26
|
+
* \ActiveRecord 3.0.3 or later. Earlier versions will not work.
|
27
|
+
|
28
|
+
Generally, \ActiveRecord adapters which depend on this module should be
|
29
|
+
installed as gems, and they will install this module automatically as
|
30
|
+
a dependency. However, you can also install it manually as a gem:
|
31
|
+
|
32
|
+
gem install rgeo
|
33
|
+
gem install rgeo-activerecord
|
34
|
+
|
35
|
+
See the README for the "rgeo" gem, a required dependency, for further
|
36
|
+
installation information.
|
37
|
+
|
38
|
+
=== To-do list
|
39
|
+
|
40
|
+
* We want to investigate writing some Arel extensions for constructing
|
41
|
+
spatial queries.
|
42
|
+
|
43
|
+
=== Development and support
|
44
|
+
|
45
|
+
Documentation is available at http://virtuoso.rubyforge.org/rgeo-activerecord/README_rdoc.html
|
46
|
+
|
47
|
+
Source code is hosted on Github at http://github.com/dazuma/rgeo-activerecord
|
48
|
+
|
49
|
+
Contributions are welcome. Fork the project on Github.
|
50
|
+
|
51
|
+
Report bugs on Github issues at http://github.org/dazuma/rgeo-activerecord/issues
|
52
|
+
|
53
|
+
Contact the author at dazuma at gmail dot com.
|
54
|
+
|
55
|
+
=== Acknowledgments
|
56
|
+
|
57
|
+
\RGeo is written by Daniel Azuma (http://www.daniel-azuma.com).
|
58
|
+
|
59
|
+
Development of \RGeo is sponsored by GeoPage, Inc. (http://www.geopage.com).
|
60
|
+
|
61
|
+
=== License
|
62
|
+
|
63
|
+
Copyright 2010 Daniel Azuma
|
64
|
+
|
65
|
+
All rights reserved.
|
66
|
+
|
67
|
+
Redistribution and use in source and binary forms, with or without
|
68
|
+
modification, are permitted provided that the following conditions are met:
|
69
|
+
|
70
|
+
* Redistributions of source code must retain the above copyright notice,
|
71
|
+
this list of conditions and the following disclaimer.
|
72
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
73
|
+
this list of conditions and the following disclaimer in the documentation
|
74
|
+
and/or other materials provided with the distribution.
|
75
|
+
* Neither the name of the copyright holder, nor the names of any other
|
76
|
+
contributors to this software, may be used to endorse or promote products
|
77
|
+
derived from this software without specific prior written permission.
|
78
|
+
|
79
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
80
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
81
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
82
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
83
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
84
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
85
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
86
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
87
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
88
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
89
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/Version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# ActiveRecord extensions for RGeo
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
# Dependencies
|
38
|
+
require 'rgeo'
|
39
|
+
require 'active_record'
|
40
|
+
|
41
|
+
|
42
|
+
# RGeo is a spatial data library for Ruby, provided by the "rgeo" gem.
|
43
|
+
#
|
44
|
+
# The optional RGeo::ActiveRecord module provides spatial extensions for
|
45
|
+
# ActiveRecord, and a set of tools and helpers for writing RGeo-based
|
46
|
+
# spatial connection adapters.
|
47
|
+
|
48
|
+
module RGeo
|
49
|
+
|
50
|
+
|
51
|
+
# This module contains a set of ActiveRecord extensions for RGeo.
|
52
|
+
# Generally, you will not need to interact with the contents of this
|
53
|
+
# module directly, unless you are writing a spatial ActiveRecord
|
54
|
+
# connection adapter.
|
55
|
+
|
56
|
+
module ActiveRecord
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
# Implementation files
|
64
|
+
require 'rgeo/active_record/common.rb'
|
65
|
+
require 'rgeo/active_record/arel_modifications'
|
66
|
+
require 'rgeo/active_record/ar_modifications'
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Helper methods for ActiveRecord adapter tests
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'rgeo/active_record'
|
38
|
+
require 'yaml'
|
39
|
+
require 'logger'
|
40
|
+
|
41
|
+
|
42
|
+
module RGeo
|
43
|
+
module ActiveRecord
|
44
|
+
|
45
|
+
|
46
|
+
# A helper module for creating unit tests for adapters.
|
47
|
+
|
48
|
+
module AdapterTestHelper
|
49
|
+
|
50
|
+
@class_num = 0
|
51
|
+
|
52
|
+
|
53
|
+
# When this module is included in a test case class, it
|
54
|
+
# automatically attempts to load the database config file from the
|
55
|
+
# path specified by the class's DATABASE_CONFIG_FILE constant.
|
56
|
+
# It then defines the DATABASE_CONFIG and DEFAULT_AR_CLASS constants
|
57
|
+
# in the testcase class.
|
58
|
+
#
|
59
|
+
# When you define your test methods, you should wrap them in a call
|
60
|
+
# to the class method define_test_methods. This will cause them to
|
61
|
+
# be defined conditionally based on whether the database config is
|
62
|
+
# present.
|
63
|
+
|
64
|
+
def self.included(klass_)
|
65
|
+
database_config_ = ::YAML.load_file(klass_.const_get(:DATABASE_CONFIG_PATH)) rescue nil
|
66
|
+
if database_config_
|
67
|
+
database_config_.symbolize_keys!
|
68
|
+
if klass_.respond_to?(:before_open_database)
|
69
|
+
klass_.before_open_database(:config => database_config_)
|
70
|
+
end
|
71
|
+
klass_.const_set(:DATABASE_CONFIG, database_config_)
|
72
|
+
ar_class_ = AdapterTestHelper.new_class(database_config_)
|
73
|
+
klass_.const_set(:DEFAULT_AR_CLASS, ar_class_)
|
74
|
+
if klass_.respond_to?(:initialize_database)
|
75
|
+
klass_.initialize_database(:ar_class => ar_class_, :connection => ar_class_.connection)
|
76
|
+
end
|
77
|
+
def klass_.define_test_methods
|
78
|
+
yield
|
79
|
+
end
|
80
|
+
else
|
81
|
+
def klass_.define_test_methods
|
82
|
+
def test_warning
|
83
|
+
puts "WARNING: Couldn't find database.yml; skipping tests."
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def self.new_class(param_) # :nodoc:
|
91
|
+
base_ = param_.kind_of?(::Class) ? param_ : ::ActiveRecord::Base
|
92
|
+
config_ = param_.kind_of?(::Hash) ? param_ : nil
|
93
|
+
klass_ = ::Class.new(base_)
|
94
|
+
@class_num += 1
|
95
|
+
self.const_set("Klass#{@class_num}".to_sym, klass_)
|
96
|
+
klass_.class_eval do
|
97
|
+
establish_connection(config_) if config_
|
98
|
+
set_table_name(:spatial_test)
|
99
|
+
end
|
100
|
+
klass_
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
# Default setup method that calls cleanup_tables.
|
105
|
+
# It also defines a couple of useful factories: @factory (a
|
106
|
+
# cartesian factory) and @geographic_factory (a spherical factory)
|
107
|
+
|
108
|
+
def setup
|
109
|
+
@factory = ::RGeo::Cartesian.preferred_factory(:srid => 4326)
|
110
|
+
@geographic_factory = ::RGeo::Geographic.spherical_factory(:srid => 4326)
|
111
|
+
cleanup_tables
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
# Default teardown method that calls cleanup_tables.
|
116
|
+
|
117
|
+
def teardown
|
118
|
+
cleanup_tables
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
# Utility method that attempts to clean up any table that was
|
123
|
+
# created by a test method. Normally called automatically at setup
|
124
|
+
# and teardown. If you override those methods, you'll need to call
|
125
|
+
# this from your method.
|
126
|
+
|
127
|
+
def cleanup_tables
|
128
|
+
klass_ = self.class.const_get(:DEFAULT_AR_CLASS)
|
129
|
+
if klass_.connection.tables.include?('spatial_test')
|
130
|
+
klass_.connection.drop_table(:spatial_test)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
# Utility method that creates and returns a new ActiveRecord class
|
136
|
+
# subclassing the DEFAULT_AR_CLASS.
|
137
|
+
|
138
|
+
def create_ar_class(opts_={})
|
139
|
+
@ar_class = AdapterTestHelper.new_class(self.class.const_get(:DEFAULT_AR_CLASS))
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Mysqlgeo adapter for ActiveRecord
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'active_record'
|
38
|
+
|
39
|
+
|
40
|
+
# RGeo extensions to ActiveRecord are installed when one of the spatial
|
41
|
+
# connection adapters is needed. These modifications require ActiveRecord
|
42
|
+
# 3.0.3 or later.
|
43
|
+
|
44
|
+
module ActiveRecord
|
45
|
+
|
46
|
+
|
47
|
+
# RGeo extends ActiveRecord::Base to include the following new class
|
48
|
+
# attributes. These attributes are inherited by subclasses, and can
|
49
|
+
# be overridden in subclasses.
|
50
|
+
#
|
51
|
+
# === ActiveRecord::Base::rgeo_factory_generator
|
52
|
+
#
|
53
|
+
# The value of this attribute is a RGeo::Feature::FactoryGenerator
|
54
|
+
# that is used to generate the proper factory when loading geometry
|
55
|
+
# objects from the database. For example, if the data being loaded
|
56
|
+
# has M but not Z coordinates, and an embedded SRID, then this
|
57
|
+
# FactoryGenerator is called with the appropriate configuration to
|
58
|
+
# obtain a factory with those properties. This factory is the one
|
59
|
+
# associated with the actual geometry properties of the ActiveRecord
|
60
|
+
# object. The result of this generator can be overridden by setting
|
61
|
+
# an explicit factory for a given class and column using the
|
62
|
+
# column_rgeo_factory method.
|
63
|
+
|
64
|
+
class Base
|
65
|
+
|
66
|
+
|
67
|
+
self.attribute_types_cached_by_default << :geometry
|
68
|
+
|
69
|
+
|
70
|
+
class_attribute :rgeo_factory_generator, :instance_writer => false
|
71
|
+
self.rgeo_factory_generator = nil
|
72
|
+
|
73
|
+
|
74
|
+
class << self
|
75
|
+
|
76
|
+
|
77
|
+
# This is a convenient way to set the rgeo_factory_generator by
|
78
|
+
# passing a block.
|
79
|
+
|
80
|
+
def to_generate_rgeo_factory(&block_)
|
81
|
+
self.rgeo_factory_generator = block_
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# Set a specific factory for this ActiveRecord class and the given
|
86
|
+
# column name. This setting, if present, overrides the result of the
|
87
|
+
# rgeo_factory_generator.
|
88
|
+
|
89
|
+
def set_rgeo_factory_for_column(column_, factory_)
|
90
|
+
@rgeo_factory_for_column = {} unless defined?(@rgeo_factory_for_column)
|
91
|
+
@rgeo_factory_for_column[column_.to_sym] = factory_
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
# Returns the factory generator or specific factory to use for this
|
96
|
+
# ActiveRecord class and the given column name.
|
97
|
+
# If an explicit factory was set for the given column, returns it.
|
98
|
+
# Otherwise, if a params hash is given, passes that has to the
|
99
|
+
# rgeo_factory_generator for this class, and returns the resulting
|
100
|
+
# factory. Otherwise, if no params hash is given, just returns the
|
101
|
+
# rgeo_factory_generator for this class.
|
102
|
+
|
103
|
+
def rgeo_factory_for_column(column_, params_=nil)
|
104
|
+
@rgeo_factory_for_column = {} unless defined?(@rgeo_factory_for_column)
|
105
|
+
result_ = @rgeo_factory_for_column[column_.to_sym] || rgeo_factory_generator || ::RGeo::ActiveRecord::DEFAULT_FACTORY_GENERATOR
|
106
|
+
if params_ && !result_.kind_of?(::RGeo::Feature::Factory::Instance)
|
107
|
+
result_ = result_.call(params_)
|
108
|
+
end
|
109
|
+
result_
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
# :stopdoc:
|
114
|
+
alias_method :columns_without_rgeo_modification, :columns
|
115
|
+
def columns
|
116
|
+
unless defined?(@columns) && @columns
|
117
|
+
columns_without_rgeo_modification.each do |column_|
|
118
|
+
column_.set_ar_class(self) if column_.respond_to?(:set_ar_class)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
@columns
|
122
|
+
end
|
123
|
+
# :startdoc:
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
module ConnectionAdapters # :nodoc:
|
131
|
+
|
132
|
+
class TableDefinition # :nodoc:
|
133
|
+
|
134
|
+
::RGeo::ActiveRecord::GEOMETRY_TYPES.each do |type_|
|
135
|
+
method_ = <<-END_METHOD
|
136
|
+
def #{type_}(*args_)
|
137
|
+
opts_ = args_.extract_options!
|
138
|
+
args_.each{ |name_| column(name_, '#{type_}', opts_) }
|
139
|
+
end
|
140
|
+
END_METHOD
|
141
|
+
class_eval(method_, __FILE__, __LINE__-5)
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Mysqlgeo adapter for ActiveRecord
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'arel'
|
38
|
+
|
39
|
+
|
40
|
+
# :stopdoc:
|
41
|
+
|
42
|
+
module Arel
|
43
|
+
|
44
|
+
module Attributes
|
45
|
+
|
46
|
+
class Geometry < Attribute; end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
alias_method :for_without_geometry, :for
|
50
|
+
def for(column_)
|
51
|
+
column_.type == :geometry ? Geometry : for_without_geometry(column_)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
module Visitors
|
58
|
+
|
59
|
+
class Dot
|
60
|
+
alias :visit_Arel_Attributes_Geometry :visit_Arel_Attribute
|
61
|
+
alias :visit_RGeo_Feature_Geometry :visit_String
|
62
|
+
end
|
63
|
+
|
64
|
+
class ToSql
|
65
|
+
alias :visit_Arel_Attributes_Geometry :visit_Arel_Attributes_Attribute
|
66
|
+
alias :visit_RGeo_Feature_Geometry :visit_String
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
# :startdoc:
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Common tools for spatial adapters for ActiveRecord
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
module RGeo
|
38
|
+
|
39
|
+
module ActiveRecord
|
40
|
+
|
41
|
+
|
42
|
+
# Additional column types for geometries.
|
43
|
+
GEOMETRY_TYPES = [:geometry, :point, :line_string, :polygon, :geometry_collection, :multi_line_string, :multi_point, :multi_polygon].freeze
|
44
|
+
|
45
|
+
# The default factory generator for ActiveRecord::Base.
|
46
|
+
DEFAULT_FACTORY_GENERATOR = ::Proc.new do |config_|
|
47
|
+
if config_.delete(:geographic)
|
48
|
+
::RGeo::Geographic.spherical_factory(config_)
|
49
|
+
else
|
50
|
+
::RGeo::Cartesian.preferred_factory(config_)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
# Index definition struct with a spatial flag field.
|
56
|
+
|
57
|
+
class SpatialIndexDefinition < ::Struct.new(:table, :name, :unique, :columns, :lengths, :spatial)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
class << self
|
62
|
+
|
63
|
+
|
64
|
+
# Returns a feature type module given a string type.
|
65
|
+
|
66
|
+
def geometric_type_from_name(name_)
|
67
|
+
case name_.downcase
|
68
|
+
when 'geometry' then ::RGeo::Feature::Geometry
|
69
|
+
when 'point' then ::RGeo::Feature::Point
|
70
|
+
when 'linestring' then ::RGeo::Feature::LineString
|
71
|
+
when 'polygon' then ::RGeo::Feature::Polygon
|
72
|
+
when 'geometrycollection' then ::RGeo::Feature::GeometryCollection
|
73
|
+
when 'multipoint' then ::RGeo::Feature::MultiPoint
|
74
|
+
when 'multilinestring' then ::RGeo::Feature::MultiLineString
|
75
|
+
when 'multipolygon' then ::RGeo::Feature::MultiPolygon
|
76
|
+
else nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
data/test/tc_basic.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# -----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# Tests for basic ActiveRecord extensions
|
4
|
+
#
|
5
|
+
# -----------------------------------------------------------------------------
|
6
|
+
# Copyright 2010 Daniel Azuma
|
7
|
+
#
|
8
|
+
# All rights reserved.
|
9
|
+
#
|
10
|
+
# Redistribution and use in source and binary forms, with or without
|
11
|
+
# modification, are permitted provided that the following conditions are met:
|
12
|
+
#
|
13
|
+
# * Redistributions of source code must retain the above copyright notice,
|
14
|
+
# this list of conditions and the following disclaimer.
|
15
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
# this list of conditions and the following disclaimer in the documentation
|
17
|
+
# and/or other materials provided with the distribution.
|
18
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
19
|
+
# contributors to this software, may be used to endorse or promote products
|
20
|
+
# derived from this software without specific prior written permission.
|
21
|
+
#
|
22
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
# -----------------------------------------------------------------------------
|
34
|
+
;
|
35
|
+
|
36
|
+
|
37
|
+
require 'test/unit'
|
38
|
+
require 'rgeo/active_record'
|
39
|
+
|
40
|
+
|
41
|
+
module RGeo
|
42
|
+
module ActiveRecord
|
43
|
+
module Tests # :nodoc:
|
44
|
+
|
45
|
+
class TestBasic < ::Test::Unit::TestCase # :nodoc:
|
46
|
+
|
47
|
+
|
48
|
+
def test_default_factory_generator
|
49
|
+
::ActiveRecord::Base.rgeo_factory_generator = nil
|
50
|
+
factory_ = ::ActiveRecord::Base.rgeo_factory_for_column(:hello).call(:has_z_coordinate => true, :srid => 4326)
|
51
|
+
assert_equal(true, factory_.property(:has_z_coordinate))
|
52
|
+
assert_equal(true, factory_.property(:is_cartesian))
|
53
|
+
assert_nil(factory_.property(:is_geographic))
|
54
|
+
assert_equal(4326, factory_.srid)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def test_set_factory_generator
|
59
|
+
::ActiveRecord::Base.rgeo_factory_generator = ::RGeo::Geographic.method(:spherical_factory)
|
60
|
+
factory_ = ::ActiveRecord::Base.rgeo_factory_for_column(:hello, :has_z_coordinate => true, :srid => 4326)
|
61
|
+
assert_equal(true, factory_.property(:has_z_coordinate))
|
62
|
+
assert_equal(true, factory_.property(:is_geographic))
|
63
|
+
assert_nil(factory_.property(:is_cartesian))
|
64
|
+
assert_equal(false, factory_.has_projection?)
|
65
|
+
assert_equal(4326, factory_.srid)
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def test_specific_factory_for_column
|
70
|
+
::ActiveRecord::Base.rgeo_factory_generator = nil
|
71
|
+
::ActiveRecord::Base.set_rgeo_factory_for_column(:foo, ::RGeo::Geographic.simple_mercator_factory(:has_z_coordinate => true))
|
72
|
+
factory_ = ::ActiveRecord::Base.rgeo_factory_for_column(:foo)
|
73
|
+
assert_equal(true, factory_.property(:has_z_coordinate))
|
74
|
+
assert_equal(true, factory_.property(:is_geographic))
|
75
|
+
assert_nil(factory_.property(:is_cartesian))
|
76
|
+
assert_equal(true, factory_.has_projection?)
|
77
|
+
assert_equal(4326, factory_.srid)
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
def test_geometry_types
|
82
|
+
[:geometry, :point, :line_string, :polygon, :geometry_collection, :multi_line_string, :multi_point, :multi_polygon].each{ |type_| assert(::RGeo::ActiveRecord::GEOMETRY_TYPES.include?(type_), "Type #{type_.inspect} not found" ) }
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rgeo-activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Daniel Azuma
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-07 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rgeo
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 2
|
31
|
+
- 0
|
32
|
+
version: 0.2.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activerecord
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 3
|
45
|
+
- 0
|
46
|
+
- 3
|
47
|
+
version: 3.0.3
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
description: RGeo is a geospatial data library for Ruby. RGeo::ActiveRecord is an optional RGeo module providing some spatial extensions to ActiveRecord, as well as common tools used by RGeo-based spatial adapters.
|
51
|
+
email: dazuma@gmail.com
|
52
|
+
executables: []
|
53
|
+
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files:
|
57
|
+
- History.rdoc
|
58
|
+
- README.rdoc
|
59
|
+
files:
|
60
|
+
- lib/rgeo/active_record/adapter_test_helper.rb
|
61
|
+
- lib/rgeo/active_record/ar_modifications.rb
|
62
|
+
- lib/rgeo/active_record/arel_modifications.rb
|
63
|
+
- lib/rgeo/active_record/common.rb
|
64
|
+
- lib/rgeo/active_record.rb
|
65
|
+
- History.rdoc
|
66
|
+
- README.rdoc
|
67
|
+
- test/tc_basic.rb
|
68
|
+
- Version
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: http://virtuoso.rubyforge.org/rgeo-activerecord
|
71
|
+
licenses: []
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 1
|
85
|
+
- 8
|
86
|
+
- 7
|
87
|
+
version: 1.8.7
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: virtuoso
|
99
|
+
rubygems_version: 1.3.7
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: An RGeo module providing spatial extensions to ActiveRecord.
|
103
|
+
test_files:
|
104
|
+
- test/tc_basic.rb
|