rgeo-activerecord 0.6.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.txt +29 -0
- data/lib/rgeo-activerecord.rb +0 -1
- data/lib/rgeo/active_record.rb +5 -64
- data/lib/rgeo/active_record/adapter_test_helper.rb +0 -56
- data/lib/rgeo/active_record/ar_factory_settings.rb +1 -81
- data/lib/rgeo/active_record/arel_spatial_queries.rb +13 -111
- data/lib/rgeo/active_record/common_adapter_elements.rb +35 -77
- data/lib/rgeo/active_record/geometry_mixin.rb +0 -49
- data/lib/rgeo/active_record/spatial_expressions.rb +5 -49
- data/lib/rgeo/active_record/version.rb +1 -52
- data/test/tc_basic.rb +2 -56
- data/test/test_helper.rb +8 -13
- metadata +21 -20
- data/Version +0 -1
- data/lib/rgeo/active_record/task_hacker.rb +0 -105
@@ -1,44 +1,5 @@
|
|
1
|
-
# -----------------------------------------------------------------------------
|
2
|
-
#
|
3
|
-
# Spatial expressions for Arel
|
4
|
-
#
|
5
|
-
# -----------------------------------------------------------------------------
|
6
|
-
# Copyright 2010-2012 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
1
|
module RGeo
|
38
|
-
|
39
2
|
module ActiveRecord
|
40
|
-
|
41
|
-
|
42
3
|
# Returns true if spatial expressions (i.e. the methods in the
|
43
4
|
# SpatialExpressions module) are supported. Generally, this is true
|
44
5
|
# if Arel is at version 2.1 or later.
|
@@ -47,16 +8,12 @@ module RGeo
|
|
47
8
|
defined?(::Arel::Nodes::NamedFunction)
|
48
9
|
end
|
49
10
|
|
50
|
-
|
51
11
|
# A set of spatial expression builders.
|
52
12
|
# These methods can be chained off other spatial expressions to form
|
53
13
|
# complex expressions.
|
54
14
|
#
|
55
15
|
# These functions require Arel 2.1 or later.
|
56
|
-
|
57
16
|
module SpatialExpressions
|
58
|
-
|
59
|
-
|
60
17
|
#--
|
61
18
|
# Generic functions
|
62
19
|
#++
|
@@ -66,7 +23,6 @@ module RGeo
|
|
66
23
|
::RGeo::ActiveRecord::SpatialNamedFunction.new(function_, [self] + args_, spatial_info_)
|
67
24
|
end
|
68
25
|
|
69
|
-
|
70
26
|
#--
|
71
27
|
# Geometry functions
|
72
28
|
#++
|
@@ -257,7 +213,6 @@ module RGeo
|
|
257
213
|
::RGeo::ActiveRecord::SpatialNamedFunction.new('ST_PointOnSurface', [self], [true, true])
|
258
214
|
end
|
259
215
|
|
260
|
-
|
261
216
|
#--
|
262
217
|
# Polygon functions
|
263
218
|
#++
|
@@ -276,7 +231,6 @@ module RGeo
|
|
276
231
|
::RGeo::ActiveRecord::SpatialNamedFunction.new('ST_InteriorRingN', [self, n_.to_i], [true, true, false])
|
277
232
|
end
|
278
233
|
|
279
|
-
|
280
234
|
#--
|
281
235
|
# GeometryCollection functions
|
282
236
|
#++
|
@@ -289,14 +243,16 @@ module RGeo
|
|
289
243
|
::RGeo::ActiveRecord::SpatialNamedFunction.new('ST_GeometryN', [self, n_.to_i], [true, true, false])
|
290
244
|
end
|
291
245
|
|
292
|
-
|
293
246
|
end
|
294
247
|
|
295
|
-
|
296
248
|
end
|
297
|
-
|
298
249
|
end
|
299
250
|
|
251
|
+
# Add tools to build spatial structures in the AST.
|
252
|
+
|
253
|
+
# Allow chaining of spatial expressions from attributes
|
254
|
+
::Arel::Attribute.send :include, ::RGeo::ActiveRecord::SpatialExpressions
|
255
|
+
|
300
256
|
|
301
257
|
module Arel
|
302
258
|
|
@@ -1,56 +1,5 @@
|
|
1
|
-
# -----------------------------------------------------------------------------
|
2
|
-
#
|
3
|
-
# Version of rgeo-activerecord
|
4
|
-
#
|
5
|
-
# -----------------------------------------------------------------------------
|
6
|
-
# Copyright 2010-2012 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
|
-
begin
|
38
|
-
require 'versionomy'
|
39
|
-
rescue ::LoadError
|
40
|
-
end
|
41
|
-
|
42
|
-
|
43
1
|
module RGeo
|
44
|
-
|
45
2
|
module ActiveRecord
|
46
|
-
|
47
|
-
# Current version of RGeo::ActiveRecord as a frozen string
|
48
|
-
VERSION_STRING = ::File.read(::File.dirname(__FILE__)+'/../../../Version').strip.freeze
|
49
|
-
|
50
|
-
# Current version of RGeo::ActiveRecord as a Versionomy object, if the
|
51
|
-
# Versionomy gem is available; otherwise equal to VERSION_STRING.
|
52
|
-
VERSION = defined?(::Versionomy) ? ::Versionomy.parse(VERSION_STRING) : VERSION_STRING
|
53
|
-
|
3
|
+
VERSION = '1.0.0'.freeze
|
54
4
|
end
|
55
|
-
|
56
5
|
end
|
data/test/tc_basic.rb
CHANGED
@@ -1,57 +1,16 @@
|
|
1
|
-
# -----------------------------------------------------------------------------
|
2
|
-
#
|
3
|
-
# Tests for basic ActiveRecord extensions
|
4
|
-
#
|
5
|
-
# -----------------------------------------------------------------------------
|
6
|
-
# Copyright 2010-2012 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
1
|
require 'test_helper'
|
38
2
|
|
39
3
|
module RGeo
|
40
4
|
module ActiveRecord
|
41
5
|
module Tests # :nodoc:
|
42
|
-
|
43
|
-
class TestBasic < ::Test::Unit::TestCase # :nodoc:
|
44
|
-
|
45
|
-
|
6
|
+
class TestBasic < MINITEST_CLASS # :nodoc:
|
46
7
|
class MyTable < ::ActiveRecord::Base
|
47
8
|
end
|
48
9
|
|
49
|
-
|
50
10
|
def test_has_version
|
51
|
-
|
11
|
+
assert ::RGeo::ActiveRecord::VERSION
|
52
12
|
end
|
53
13
|
|
54
|
-
|
55
14
|
def test_default_factory_generator
|
56
15
|
MyTable.rgeo_factory_generator = nil
|
57
16
|
factory_ = MyTable.rgeo_factory_for_column(:hello).call(:has_z_coordinate => true, :srid => 4326)
|
@@ -61,7 +20,6 @@ module RGeo
|
|
61
20
|
assert_equal(4326, factory_.srid)
|
62
21
|
end
|
63
22
|
|
64
|
-
|
65
23
|
def test_set_factory_generator
|
66
24
|
MyTable.rgeo_factory_generator = ::RGeo::Geographic.method(:spherical_factory)
|
67
25
|
factory_ = MyTable.rgeo_factory_for_column(:hello, :has_z_coordinate => true, :srid => 4326)
|
@@ -72,7 +30,6 @@ module RGeo
|
|
72
30
|
assert_equal(4326, factory_.srid)
|
73
31
|
end
|
74
32
|
|
75
|
-
|
76
33
|
def test_specific_factory_for_column
|
77
34
|
MyTable.rgeo_factory_generator = nil
|
78
35
|
MyTable.set_rgeo_factory_for_column(:foo, ::RGeo::Geographic.simple_mercator_factory(:has_z_coordinate => true))
|
@@ -84,7 +41,6 @@ module RGeo
|
|
84
41
|
assert_equal(4326, factory_.srid)
|
85
42
|
end
|
86
43
|
|
87
|
-
|
88
44
|
def test_default_as_json_wkt
|
89
45
|
GeometryMixin.set_json_generator(nil)
|
90
46
|
factory_ = ::RGeo::Cartesian.preferred_factory
|
@@ -92,7 +48,6 @@ module RGeo
|
|
92
48
|
assert_equal("POINT (1.0 2.0)", p_.as_json)
|
93
49
|
end
|
94
50
|
|
95
|
-
|
96
51
|
def test_default_as_json_geojson
|
97
52
|
GeometryMixin.set_json_generator(:geojson)
|
98
53
|
factory_ = ::RGeo::Cartesian.preferred_factory
|
@@ -105,15 +60,6 @@ module RGeo
|
|
105
60
|
sql = visitor.accept(Arel.spatial('POINT (1.0 2.0)'))
|
106
61
|
assert_equal("ST_WKTToSQL('POINT (1.0 2.0)')", sql)
|
107
62
|
end
|
108
|
-
|
109
|
-
# SpatialNamedFunction is not used in pre-2.1 Arel.
|
110
|
-
if(AREL_VERSION_MAJOR > 2 || (AREL_VERSION_MAJOR == 2 && AREL_VERSION_MINOR >= 1))
|
111
|
-
def test_arel_visit_SpatialNamedFunction
|
112
|
-
visitor = arel_visitor
|
113
|
-
sql = visitor.accept(Arel.spatial('POINT (1.0 2.0)').st_astext)
|
114
|
-
assert_equal("ST_AsText(ST_WKTToSQL('POINT (1.0 2.0)'))", sql)
|
115
|
-
end
|
116
|
-
end
|
117
63
|
end
|
118
64
|
end
|
119
65
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,21 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'minitest/autorun'
|
2
2
|
require 'rgeo/active_record'
|
3
3
|
require 'support/fake_record'
|
4
4
|
|
5
|
-
|
5
|
+
MINITEST_CLASS = if defined?(::Minitest::Test)
|
6
|
+
::Minitest::Test
|
7
|
+
else
|
8
|
+
::Minitest::Unit::TestCase
|
9
|
+
end
|
10
|
+
|
6
11
|
Arel::Visitors::PostgreSQL.send(:include, ::RGeo::ActiveRecord::SpatialToSql)
|
7
12
|
Arel::Table.engine = Arel::Sql::Engine.new(FakeRecord::Base.new)
|
8
13
|
|
9
14
|
def arel_visitor
|
10
|
-
|
11
|
-
# Arel.
|
12
|
-
if(AREL_VERSION_MAJOR <= 2)
|
13
|
-
if(AREL_VERSION_MAJOR == 2 && AREL_VERSION_MINOR == 0)
|
14
|
-
Arel::Visitors::PostgreSQL.new(Arel::Table.engine)
|
15
|
-
else
|
16
|
-
Arel::Visitors::PostgreSQL.new(Arel::Table.engine.connection_pool)
|
17
|
-
end
|
18
|
-
else
|
19
|
-
Arel::Visitors::PostgreSQL.new(Arel::Table.engine.connection)
|
20
|
-
end
|
15
|
+
Arel::Visitors::PostgreSQL.new(Arel::Table.engine.connection)
|
21
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rgeo-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Azuma
|
@@ -14,58 +14,58 @@ dependencies:
|
|
14
14
|
name: rgeo
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 0.3.20
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.3.20
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activerecord
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '4.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '4.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
48
|
-
type: :
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rake
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '10.2'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '10.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rdoc
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -91,8 +91,8 @@ extra_rdoc_files:
|
|
91
91
|
- README.rdoc
|
92
92
|
files:
|
93
93
|
- History.rdoc
|
94
|
+
- LICENSE.txt
|
94
95
|
- README.rdoc
|
95
|
-
- Version
|
96
96
|
- lib/rgeo-activerecord.rb
|
97
97
|
- lib/rgeo/active_record.rb
|
98
98
|
- lib/rgeo/active_record/adapter_test_helper.rb
|
@@ -101,12 +101,11 @@ files:
|
|
101
101
|
- lib/rgeo/active_record/common_adapter_elements.rb
|
102
102
|
- lib/rgeo/active_record/geometry_mixin.rb
|
103
103
|
- lib/rgeo/active_record/spatial_expressions.rb
|
104
|
-
- lib/rgeo/active_record/task_hacker.rb
|
105
104
|
- lib/rgeo/active_record/version.rb
|
106
105
|
- test/support/fake_record.rb
|
107
106
|
- test/tc_basic.rb
|
108
107
|
- test/test_helper.rb
|
109
|
-
homepage: http://
|
108
|
+
homepage: http://rgeo.github.com/rgeo-activerecord
|
110
109
|
licenses: []
|
111
110
|
metadata: {}
|
112
111
|
post_install_message:
|
@@ -117,18 +116,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
116
|
requirements:
|
118
117
|
- - ">="
|
119
118
|
- !ruby/object:Gem::Version
|
120
|
-
version: 1.
|
119
|
+
version: 1.9.3
|
121
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
121
|
requirements:
|
123
122
|
- - ">="
|
124
123
|
- !ruby/object:Gem::Version
|
125
124
|
version: '0'
|
126
125
|
requirements: []
|
127
|
-
rubyforge_project:
|
126
|
+
rubyforge_project:
|
128
127
|
rubygems_version: 2.2.2
|
129
128
|
signing_key:
|
130
129
|
specification_version: 4
|
131
130
|
summary: An RGeo module providing spatial extensions to ActiveRecord.
|
132
131
|
test_files:
|
132
|
+
- test/support/fake_record.rb
|
133
133
|
- test/tc_basic.rb
|
134
|
+
- test/test_helper.rb
|
134
135
|
has_rdoc:
|
data/Version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.6.0
|
@@ -1,105 +0,0 @@
|
|
1
|
-
# -----------------------------------------------------------------------------
|
2
|
-
#
|
3
|
-
# A tool for hacking ActiveRecord's rake tasks
|
4
|
-
#
|
5
|
-
# -----------------------------------------------------------------------------
|
6
|
-
# Copyright 2010-2012 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
|
-
# A set of tools for hacking ActiveRecord's Rake tasks.
|
43
|
-
|
44
|
-
module TaskHacker
|
45
|
-
|
46
|
-
|
47
|
-
class Action # :nodoc:
|
48
|
-
|
49
|
-
def initialize(env_, pattern_, proc_)
|
50
|
-
@env = env_
|
51
|
-
@pattern = pattern_
|
52
|
-
@proc = proc_
|
53
|
-
end
|
54
|
-
|
55
|
-
def call(task_)
|
56
|
-
env_ = @env || ::Rails.env || 'development'
|
57
|
-
config_ = ::ActiveRecord::Base.configurations[env_]
|
58
|
-
if config_
|
59
|
-
if @pattern === config_['adapter']
|
60
|
-
task_.actions.delete_if{ |a_| a_ != self }
|
61
|
-
@proc.call(config_)
|
62
|
-
end
|
63
|
-
else
|
64
|
-
puts "WARNING: Could not find environment #{env_.inspect} in your database.yml"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def arity
|
69
|
-
1
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
|
-
|
75
|
-
class << self
|
76
|
-
|
77
|
-
|
78
|
-
# Modify a named ActiveRecord rake task.
|
79
|
-
# The task must be of the form that hinges on the database adapter
|
80
|
-
# name. You must provide the fully-qualified name of the rake task
|
81
|
-
# to modify, the Rails environment for which to get the database
|
82
|
-
# configuration (which may be nil to use the current Rails.env),
|
83
|
-
# a Regexp or String identifying the adapter name for which to
|
84
|
-
# modify the rake task, and a block. If the database adapter
|
85
|
-
# associated with the given environment matches the given pattern,
|
86
|
-
# then the rake task's action(s) will be replaced by the given
|
87
|
-
# block. The block will be passed the environment's database
|
88
|
-
# configuration hash.
|
89
|
-
|
90
|
-
def modify(name_, env_, pattern_, &block_)
|
91
|
-
if ::Rake::Task.task_defined?(name_)
|
92
|
-
::Rake::Task[name_].actions.unshift(Action.new(env_, pattern_, block_))
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|