rgeo-activerecord 0.3.3 → 0.3.4
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.
- data/History.rdoc +4 -0
- data/README.rdoc +6 -6
- data/Version +1 -1
- data/lib/rgeo/active_record.rb +1 -0
- data/lib/rgeo/active_record/geometry_mixin.rb +108 -0
- data/test/tc_basic.rb +16 -0
- metadata +5 -4
data/History.rdoc
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
=== 0.3.4 / 2011-05-23
|
2
|
+
|
3
|
+
* Uses the mixin feature of RGeo 0.3 to add an as_json method to all geometry objects. This should allow ActiveRecord's JSON serialization to function for models with geometry fields. (Reported by thenetduck and tonyc on github.)
|
4
|
+
|
1
5
|
=== 0.3.3 / 2011-04-11
|
2
6
|
|
3
7
|
* A .gemspec file is now available for gem building and bundler git integration.
|
data/README.rdoc
CHANGED
@@ -16,14 +16,14 @@ along with a set of geometric analysis operations. See the README for the
|
|
16
16
|
RGeo::ActiveRecord is an optional \RGeo add-on module providing spatial
|
17
17
|
extensions for \ActiveRecord, as well as a set of helpers for writing
|
18
18
|
spatial \ActiveRecord adapters based on \RGeo. Generally, you will not
|
19
|
-
interact directly with this library
|
19
|
+
need to interact directly with this library.
|
20
20
|
|
21
21
|
=== Installation
|
22
22
|
|
23
23
|
RGeo::ActiveRecord has the following requirements:
|
24
24
|
|
25
25
|
* Ruby 1.8.7 or later. Ruby 1.9.2 or later preferred.
|
26
|
-
* \RGeo 0.
|
26
|
+
* \RGeo 0.3.0 or later.
|
27
27
|
* \ActiveRecord 3.0.3 or later. Earlier versions will not work.
|
28
28
|
* \Arel 2.0.6 or later. Earlier versions will not work.
|
29
29
|
|
@@ -39,10 +39,10 @@ installation information.
|
|
39
39
|
|
40
40
|
=== To-do list
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
We consider this gem to be in a late alpha state. There are a still a
|
43
|
+
few holes in the intended functionality, but it is now being tested in a
|
44
|
+
variety of contexts, and we know of several successful production
|
45
|
+
deployments.
|
46
46
|
|
47
47
|
We are also currently working on a mechanism for constructing complex
|
48
48
|
spatial queries via extensions to Arel. This work is in the experimental
|
data/Version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.4
|
data/lib/rgeo/active_record.rb
CHANGED
@@ -0,0 +1,108 @@
|
|
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
|
+
# This module is mixed into all geometry objects. It provides an
|
43
|
+
# as_json method so that ActiveRecord knows how to generate JSON
|
44
|
+
# for a geometry-valued field.
|
45
|
+
|
46
|
+
module GeometryMixin
|
47
|
+
|
48
|
+
|
49
|
+
# The default JSON generator Proc. Renders geometry fields as WKT.
|
50
|
+
DEFAULT_JSON_GENERATOR = ::Proc.new{ |geom_| geom_.to_s }
|
51
|
+
|
52
|
+
@json_generator = DEFAULT_JSON_GENERATOR
|
53
|
+
|
54
|
+
|
55
|
+
# Set the style of JSON generation used for geometry fields in an
|
56
|
+
# ActiveRecord model by default. You may pass nil to use
|
57
|
+
# DEFAULT_JSON_GENERATOR, a proc that takes a geometry as the
|
58
|
+
# argument and returns an object that can be converted to JSON
|
59
|
+
# (i.e. usually a hash or string), or one of the following symbolic
|
60
|
+
# values:
|
61
|
+
#
|
62
|
+
# <tt>:wkt</tt>::
|
63
|
+
# Well-known text format. (Same as DEFAULT_JSON_GENERATOR.)
|
64
|
+
# <tt>:geojson</tt>::
|
65
|
+
# GeoJSON format. Requires the rgeo-geojson gem.
|
66
|
+
|
67
|
+
def self.set_json_generator(value_=nil, &block_)
|
68
|
+
if block_ && !value_
|
69
|
+
value_ = block_
|
70
|
+
elsif value_ == :geojson
|
71
|
+
require 'rgeo/geo_json'
|
72
|
+
value_ = ::Proc.new{ |geom_| ::RGeo::GeoJSON.encode(geom_) }
|
73
|
+
end
|
74
|
+
if value_.is_a?(::Proc)
|
75
|
+
@json_generator = value_
|
76
|
+
else
|
77
|
+
@json_generator = DEFAULT_JSON_GENERATOR
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
# Given a feature, returns an object that can be serialized as JSON
|
83
|
+
# (i.e. usually a hash or string), using the current json_generator.
|
84
|
+
# This is used to generate JSON for geometry-valued ActiveRecord
|
85
|
+
# fields by default.
|
86
|
+
|
87
|
+
def self.generate_json(geom_)
|
88
|
+
@json_generator.call(geom_)
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
# Serializes this object as JSON for ActiveRecord.
|
93
|
+
|
94
|
+
def as_json(opts_=nil)
|
95
|
+
GeometryMixin.generate_json(self)
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
::RGeo::Feature::MixinCollection::GLOBAL.for_type(::RGeo::Feature::Geometry).
|
108
|
+
add(::RGeo::ActiveRecord::GeometryMixin)
|
data/test/tc_basic.rb
CHANGED
@@ -83,6 +83,22 @@ module RGeo
|
|
83
83
|
end
|
84
84
|
|
85
85
|
|
86
|
+
def test_default_as_json_wkt
|
87
|
+
GeometryMixin.set_json_generator(nil)
|
88
|
+
factory_ = ::RGeo::Cartesian.preferred_factory
|
89
|
+
p_ = factory_.point(1, 2)
|
90
|
+
assert_equal("POINT (1.0 2.0)", p_.as_json)
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def test_default_as_json_geojson
|
95
|
+
GeometryMixin.set_json_generator(:geojson)
|
96
|
+
factory_ = ::RGeo::Cartesian.preferred_factory
|
97
|
+
p_ = factory_.point(1, 2)
|
98
|
+
assert_equal({'type' => 'Point', 'coordinates' => [1.0, 2.0]}, p_.as_json)
|
99
|
+
end
|
100
|
+
|
101
|
+
|
86
102
|
end
|
87
103
|
|
88
104
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rgeo-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Daniel Azuma
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-23 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rgeo
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.
|
23
|
+
version: 0.3.0
|
24
24
|
type: :runtime
|
25
25
|
version_requirements: *id001
|
26
26
|
- !ruby/object:Gem::Dependency
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/rgeo/active_record/ar_factory_settings.rb
|
60
60
|
- lib/rgeo/active_record/arel_spatial_queries.rb
|
61
61
|
- lib/rgeo/active_record/common_adapter_elements.rb
|
62
|
+
- lib/rgeo/active_record/geometry_mixin.rb
|
62
63
|
- lib/rgeo/active_record/spatial_expressions.rb
|
63
64
|
- lib/rgeo/active_record/task_hacker.rb
|
64
65
|
- lib/rgeo/active_record/version.rb
|
@@ -90,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
91
|
requirements: []
|
91
92
|
|
92
93
|
rubyforge_project: virtuoso
|
93
|
-
rubygems_version: 1.
|
94
|
+
rubygems_version: 1.8.2
|
94
95
|
signing_key:
|
95
96
|
specification_version: 3
|
96
97
|
summary: An RGeo module providing spatial extensions to ActiveRecord.
|