Pr0d1r2-geokit-cache 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/generators/geokit_cache/geokit_cache_generator.rb +27 -0
- data/generators/geokit_cache/templates/model.rb +5 -0
- data/generators/geokit_cache/templates/model_spec.rb +17 -0
- data/lib/geokit-cache.rb +2 -0
- data/lib/geokit/cache/active_record.rb +26 -0
- data/lib/geokit/cache/model.rb +108 -0
- data/spec/geokit-cache_spec.rb +267 -0
- data/spec/spec_helper.rb +9 -0
- metadata +108 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Marcin Nowicki
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= geokit-cache
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Marcin Nowicki. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "geokit-cache"
|
8
|
+
gem.summary = %Q{Geokit caching support }
|
9
|
+
gem.description = %Q{Caching support in database for locations geocoded by andre/geokit-gem}
|
10
|
+
gem.email = "pr0d1r2@ragnarson.com"
|
11
|
+
gem.homepage = "http://github.com/Pr0d1r2/geokit-cache"
|
12
|
+
gem.authors = ["Marcin Nowicki"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
gem.add_development_dependency "Pr0d1r2-active_record_connectionless"
|
15
|
+
gem.add_development_dependency "Pr0d1r2-active_record_geocodable"
|
16
|
+
gem.add_development_dependency "htmlentities"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :spec => :check_dependencies
|
36
|
+
|
37
|
+
task :default => :spec
|
38
|
+
|
39
|
+
require 'rake/rdoctask'
|
40
|
+
Rake::RDocTask.new do |rdoc|
|
41
|
+
if File.exist?('VERSION')
|
42
|
+
version = File.read('VERSION')
|
43
|
+
else
|
44
|
+
version = ""
|
45
|
+
end
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "geokit-cache #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class GeokitCachedGenerator < RspecModelGenerator
|
2
|
+
|
3
|
+
def initialize(runtime_args, runtime_options = {})
|
4
|
+
runtime_args = [
|
5
|
+
'GeokitCache',
|
6
|
+
|
7
|
+
'complete_address:string',
|
8
|
+
|
9
|
+
'provider:string',
|
10
|
+
'lng:float',
|
11
|
+
'lat:float',
|
12
|
+
'full_address:string',
|
13
|
+
'state:string',
|
14
|
+
'success:boolean',
|
15
|
+
'accuracy:integer',
|
16
|
+
'city:string',
|
17
|
+
'country_code:string',
|
18
|
+
'precision:string',
|
19
|
+
'street_address:string',
|
20
|
+
'zip:string',
|
21
|
+
|
22
|
+
'complete:boolean'
|
23
|
+
]
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
|
2
|
+
|
3
|
+
describe <%= class_name %> do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@valid_attributes = {
|
7
|
+
<% attributes.each_with_index do |attribute, attribute_index| -%>
|
8
|
+
:<%= attribute.name %> => <%= attribute.default_value %><%= attribute_index == attributes.length - 1 ? '' : ','%>
|
9
|
+
<% end -%>
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a new instance given valid attributes" do
|
14
|
+
<%= class_name %>.create!(@valid_attributes)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/lib/geokit-cache.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record_geocodable'
|
2
|
+
|
3
|
+
module Geokit
|
4
|
+
module Cache
|
5
|
+
module ActiveRecord
|
6
|
+
|
7
|
+
def acts_as_geokit_cache
|
8
|
+
include Geokit::Cache::Model
|
9
|
+
extend Geokit::Cache::Model::ClassMethods
|
10
|
+
is_geocodable :require => true
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_geocodable(options = {})
|
14
|
+
super
|
15
|
+
if options[:cache]
|
16
|
+
define_method(:geocoder) do
|
17
|
+
options[:cache].to_s.singularize.camelize.constantize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveRecord::Base.extend(Geokit::Cache::ActiveRecord)
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'htmlentities'
|
3
|
+
require 'active_record_geocodable'
|
4
|
+
|
5
|
+
module Geokit
|
6
|
+
module Cache
|
7
|
+
module Model
|
8
|
+
|
9
|
+
GEO_ATTRIBUTES = [
|
10
|
+
:provider,
|
11
|
+
:lng,
|
12
|
+
:lat,
|
13
|
+
:full_address,
|
14
|
+
:state,
|
15
|
+
:success,
|
16
|
+
:accuracy,
|
17
|
+
:city,
|
18
|
+
:country_code,
|
19
|
+
:precision,
|
20
|
+
:street_address,
|
21
|
+
:zip
|
22
|
+
]
|
23
|
+
TEXT_ATTRIBUTES = [:city, :state, :full_address, :street_address]
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.class_eval do
|
27
|
+
before_save :decode_html_entities_in_text_attributes
|
28
|
+
is_geocodable :require => true
|
29
|
+
base.extend(ClassMethods)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def cache!(attributes)
|
34
|
+
self.attributes = attributes
|
35
|
+
save if new_record? || changed?
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_instance_variables_from_geo!
|
39
|
+
GEO_ATTRIBUTES.each do |geo_attribute|
|
40
|
+
instance_variable_set(geo_attribute, geo.send(geo_attribute))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def update_action!
|
45
|
+
set_instance_variables_from_geo!
|
46
|
+
save if changed?
|
47
|
+
end
|
48
|
+
|
49
|
+
def needs_update?
|
50
|
+
!by_google? && geocoding_successful?
|
51
|
+
end
|
52
|
+
|
53
|
+
def update!
|
54
|
+
update_action! if needs_update?
|
55
|
+
end
|
56
|
+
|
57
|
+
def update_and_return!
|
58
|
+
update!
|
59
|
+
geoloc
|
60
|
+
end
|
61
|
+
|
62
|
+
def fake_geoloc
|
63
|
+
geoloc = Geokit::GeoLoc.new
|
64
|
+
GEO_ATTRIBUTES.each do |geo_attribute|
|
65
|
+
geoloc.instance_variable_set(geo_attribute, instance_variable_get(geo_attribute))
|
66
|
+
end
|
67
|
+
geoloc.success = success?
|
68
|
+
geoloc
|
69
|
+
end
|
70
|
+
|
71
|
+
def successful_geoloc
|
72
|
+
geo if geocoding_successful?
|
73
|
+
end
|
74
|
+
|
75
|
+
def geoloc
|
76
|
+
successful_geoloc || fake_geoloc
|
77
|
+
end
|
78
|
+
|
79
|
+
def by_google?
|
80
|
+
provider == 'google'
|
81
|
+
end
|
82
|
+
|
83
|
+
def changed_to_google?
|
84
|
+
by_google? && provider_changed?
|
85
|
+
end
|
86
|
+
|
87
|
+
def changed?
|
88
|
+
lat_changed? || lng_changed? || changed_to_google?
|
89
|
+
end
|
90
|
+
|
91
|
+
def decode_html_entities_in_text_attributes
|
92
|
+
TEXT_ATTRIBUTES.each {|a| self.send("#{a}=", HTMLEntities.new.decode(self.send(a)))} if geocoding_successful?
|
93
|
+
end
|
94
|
+
|
95
|
+
module ClassMethods
|
96
|
+
def geocode(complete_address)
|
97
|
+
record = find_or_create(complete_address)
|
98
|
+
record.update_and_return!
|
99
|
+
end
|
100
|
+
|
101
|
+
def find_or_create(complete_address)
|
102
|
+
find_by_complete_address(complete_address) || new(:complete_address => complete_address)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,267 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'active_record_connectionless'
|
3
|
+
|
4
|
+
class GeokitCache < ActiveRecord::Base
|
5
|
+
acts_as_geokit_cache
|
6
|
+
emulate_attribute :complete_address
|
7
|
+
end
|
8
|
+
|
9
|
+
class Example < ActiveRecord::Base
|
10
|
+
is_geocodable :cache => :geokit_cache
|
11
|
+
attr_accessor :complete_address
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
describe GeokitCache do
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
@geokit_cache = GeokitCache.new
|
19
|
+
@geo = mock(Geokit::GeoLoc)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'cache!' do
|
23
|
+
before(:each) do
|
24
|
+
@attributes = {:lat => 47, :lng => 47}
|
25
|
+
@geokit_cache.should_receive(:attributes=).with(@attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should save given attributes when record is new' do
|
29
|
+
@geokit_cache.should_receive(:save).and_return(true)
|
30
|
+
@geokit_cache.should_receive(:new_record?).and_return(true)
|
31
|
+
@geokit_cache.cache!(@attributes)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should save given attributes when record changed' do
|
35
|
+
@geokit_cache.should_receive(:save).and_return(true)
|
36
|
+
@geokit_cache.should_receive(:new_record?).and_return(false)
|
37
|
+
@geokit_cache.should_receive(:changed?).and_return(true)
|
38
|
+
@geokit_cache.cache!(@attributes)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should not save given attributes when record is new nor changed' do
|
42
|
+
@geokit_cache.should_not_receive(:save)
|
43
|
+
@geokit_cache.should_receive(:new_record?).and_return(false)
|
44
|
+
@geokit_cache.should_receive(:changed?).and_return(false)
|
45
|
+
@geokit_cache.cache!(@attributes)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'set_instance_variables_from_geo! should set instance variables located in geo' do
|
50
|
+
@geokit_cache.stub!(:geo).and_return(@geo)
|
51
|
+
GeokitCache::GEO_ATTRIBUTES.each do |geo_attribute|
|
52
|
+
@geo.should_receive(:send).with(geo_attribute).and_return(geo_attribute)
|
53
|
+
@geokit_cache.should_receive(:instance_variable_set).with(geo_attribute, geo_attribute)
|
54
|
+
end
|
55
|
+
@geokit_cache.set_instance_variables_from_geo!
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'update_action!' do
|
59
|
+
before(:each) do
|
60
|
+
@geokit_cache.stub!(:set_instance_variables_from_geo!)
|
61
|
+
@geokit_cache.stub!(:changed? => false)
|
62
|
+
@geokit_cache.update_action!
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should set instance variables located in geo' do
|
66
|
+
@geokit_cache.should_receive(:set_instance_variables_from_geo!)
|
67
|
+
@geokit_cache.update_action!
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should save when record changed' do
|
71
|
+
@geokit_cache.should_receive(:changed?).and_return(true)
|
72
|
+
@geokit_cache.should_receive(:save).and_return(true)
|
73
|
+
@geokit_cache.update_action!
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should not save when record not changed' do
|
77
|
+
@geokit_cache.should_receive(:changed?).and_return(false)
|
78
|
+
@geokit_cache.should_not_receive(:save)
|
79
|
+
@geokit_cache.update_action!
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'needs_update?' do
|
84
|
+
it 'should be true when record not from google and geo was successful' do
|
85
|
+
@geokit_cache.should_receive(:by_google?).and_return(false)
|
86
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(true)
|
87
|
+
@geokit_cache.needs_update?.should be_true
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should be false when record already from google' do
|
91
|
+
@geokit_cache.should_receive(:by_google?).and_return(true)
|
92
|
+
@geokit_cache.needs_update?.should be_false
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should be false when record not from google and geo was not successful' do
|
96
|
+
@geokit_cache.should_receive(:by_google?).and_return(false)
|
97
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(false)
|
98
|
+
@geokit_cache.needs_update?.should be_false
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'update!' do
|
103
|
+
it 'should update when needed' do
|
104
|
+
@geokit_cache.should_receive(:needs_update?).and_return(true)
|
105
|
+
@geokit_cache.should_receive(:update_action!).and_return(true)
|
106
|
+
@geokit_cache.update!.should be_true
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should not update when not needed' do
|
110
|
+
@geokit_cache.should_receive(:needs_update?).and_return(false)
|
111
|
+
@geokit_cache.should_not_receive(:update_action!)
|
112
|
+
@geokit_cache.update!.should be_nil
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'update_and_return! should update record and return geoloc' do
|
117
|
+
@geokit_cache.should_receive(:update!).and_return(true)
|
118
|
+
@geokit_cache.should_receive(:geoloc).and_return(:geoloc)
|
119
|
+
@geokit_cache.update_and_return!.should == :geoloc
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'fake_geoloc should return geoloc with attributes from record' do
|
123
|
+
GeokitCache::GEO_ATTRIBUTES.each do |geo_attribute|
|
124
|
+
@geokit_cache.should_receive(:instance_variable_get).with(geo_attribute).and_return(geo_attribute)
|
125
|
+
@geo.should_receive(:instance_variable_set).with(geo_attribute, geo_attribute)
|
126
|
+
end
|
127
|
+
@geokit_cache.should_receive(:success?).and_return(true)
|
128
|
+
@geo.should_receive(:success=).with(true)
|
129
|
+
Geokit::GeoLoc.should_receive(:new).and_return(@geo)
|
130
|
+
@geokit_cache.fake_geoloc.should == @geo
|
131
|
+
end
|
132
|
+
|
133
|
+
describe 'successful_geoloc' do
|
134
|
+
it 'should return geo when it is successful' do
|
135
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(true)
|
136
|
+
@geokit_cache.should_receive(:geo).and_return(:geo)
|
137
|
+
@geokit_cache.successful_geoloc.should == :geo
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should return nil when geo is not successful' do
|
141
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(false)
|
142
|
+
@geokit_cache.successful_geoloc.should be_nil
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe 'geoloc' do
|
147
|
+
it 'should return successful geoloc' do
|
148
|
+
@geokit_cache.should_receive(:successful_geoloc).and_return(:successful_geoloc)
|
149
|
+
@geokit_cache.geoloc.should == :successful_geoloc
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should return fake geoloc when there was no successful geoloc' do
|
153
|
+
@geokit_cache.should_receive(:successful_geoloc).and_return(nil)
|
154
|
+
@geokit_cache.should_receive(:fake_geoloc).and_return(:fake_geoloc)
|
155
|
+
@geokit_cache.geoloc.should == :fake_geoloc
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe 'by_google?' do
|
160
|
+
it 'should be true when provider is google' do
|
161
|
+
@geokit_cache.should_receive(:provider).and_return('google')
|
162
|
+
@geokit_cache.by_google?.should be_true
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should be true when provider is not google' do
|
166
|
+
@geokit_cache.should_receive(:provider).and_return('yahoo')
|
167
|
+
@geokit_cache.by_google?.should be_false
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe 'changed_to_google?' do
|
172
|
+
it 'should be true when geocoded by google and provider have been changed' do
|
173
|
+
@geokit_cache.should_receive(:by_google?).and_return(true)
|
174
|
+
@geokit_cache.should_receive(:provider_changed?).and_return(true)
|
175
|
+
@geokit_cache.changed_to_google?.should be_true
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'should be false when geocoded by google but provider have not been changed' do
|
179
|
+
@geokit_cache.should_receive(:by_google?).and_return(true)
|
180
|
+
@geokit_cache.should_receive(:provider_changed?).and_return(false)
|
181
|
+
@geokit_cache.changed_to_google?.should be_false
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'should be false when not geocoded by google' do
|
185
|
+
@geokit_cache.should_receive(:by_google?).and_return(false)
|
186
|
+
@geokit_cache.changed_to_google?.should be_false
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
describe 'changed?' do
|
191
|
+
before(:each) do
|
192
|
+
@geokit_cache.stub!(:lat_changed? => false, :lng_changed? => false, :changed_to_google? => false)
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'should be true when lat changed' do
|
196
|
+
@geokit_cache.should_receive(:lat_changed?).and_return(true)
|
197
|
+
@geokit_cache.changed?.should be_true
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'should be true when lng changed' do
|
201
|
+
@geokit_cache.should_receive(:lng_changed?).and_return(true)
|
202
|
+
@geokit_cache.changed?.should be_true
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'should be true when changed to google' do
|
206
|
+
@geokit_cache.should_receive(:changed_to_google?).and_return(true)
|
207
|
+
@geokit_cache.changed?.should be_true
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'should be false when none of requested changes occurs' do
|
211
|
+
@geokit_cache.changed?.should be_false
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
describe 'decode_html_entities_in_text_attributes' do
|
217
|
+
it 'should decode html-entities in all text attributes when geocoding was successful' do
|
218
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(true)
|
219
|
+
GeokitCache::TEXT_ATTRIBUTES.each do |text_attribute|
|
220
|
+
@geokit_cache.should_receive(:send).with(text_attribute).and_return(text_attribute)
|
221
|
+
@geokit_cache.should_receive(:send).with("#{text_attribute}=", text_attribute.to_s)
|
222
|
+
end
|
223
|
+
@geokit_cache.decode_html_entities_in_text_attributes
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'should not decode html-entities in any text attributes when geocoding was not successful' do
|
227
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(false)
|
228
|
+
GeokitCache::TEXT_ATTRIBUTES.each do |text_attribute|
|
229
|
+
@geokit_cache.should_not_receive(:send).with(text_attribute)
|
230
|
+
@geokit_cache.should_not_receive(:send).with("#{text_attribute}=", text_attribute.to_s)
|
231
|
+
end
|
232
|
+
@geokit_cache.decode_html_entities_in_text_attributes
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe 'class method' do
|
237
|
+
it 'geocode should return geoloc for updated or new record' do
|
238
|
+
record = mock(GeokitCache)
|
239
|
+
record.should_receive(:update_and_return!)
|
240
|
+
GeokitCache.should_receive(:find_or_create).with(:complete_address).and_return(record)
|
241
|
+
GeokitCache.geocode(:complete_address)
|
242
|
+
end
|
243
|
+
|
244
|
+
describe 'find_or_create' do
|
245
|
+
it 'should return existing record' do
|
246
|
+
GeokitCache.should_receive(:find_by_complete_address).with(:complete_address).and_return(:record)
|
247
|
+
GeokitCache.find_or_create(:complete_address).should == :record
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'should create new record when record does not exist in database' do
|
251
|
+
GeokitCache.should_receive(:find_by_complete_address).with(:complete_address)
|
252
|
+
GeokitCache.should_receive(:new).with(:complete_address => :complete_address).and_return(:new_record)
|
253
|
+
GeokitCache.find_or_create(:complete_address).should == :new_record
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
|
261
|
+
describe Example do
|
262
|
+
|
263
|
+
it 'geocoder should be GeokitCache' do
|
264
|
+
Example.new.geocoder.should == GeokitCache
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: Pr0d1r2-geokit-cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Nowicki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-23 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: Pr0d1r2-active_record_connectionless
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: Pr0d1r2-active_record_geocodable
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: htmlentities
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
description: Caching support in database for locations geocoded by andre/geokit-gem
|
56
|
+
email: pr0d1r2@ragnarson.com
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
61
|
+
extra_rdoc_files:
|
62
|
+
- LICENSE
|
63
|
+
- README.rdoc
|
64
|
+
files:
|
65
|
+
- .document
|
66
|
+
- .gitignore
|
67
|
+
- LICENSE
|
68
|
+
- README.rdoc
|
69
|
+
- Rakefile
|
70
|
+
- VERSION
|
71
|
+
- generators/geokit_cache/geokit_cache_generator.rb
|
72
|
+
- generators/geokit_cache/templates/model.rb
|
73
|
+
- generators/geokit_cache/templates/model_spec.rb
|
74
|
+
- lib/geokit-cache.rb
|
75
|
+
- lib/geokit/cache/active_record.rb
|
76
|
+
- lib/geokit/cache/model.rb
|
77
|
+
- spec/geokit-cache_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
has_rdoc: true
|
80
|
+
homepage: http://github.com/Pr0d1r2/geokit-cache
|
81
|
+
licenses:
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options:
|
84
|
+
- --charset=UTF-8
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
version:
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: "0"
|
98
|
+
version:
|
99
|
+
requirements: []
|
100
|
+
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.3.5
|
103
|
+
signing_key:
|
104
|
+
specification_version: 2
|
105
|
+
summary: Geokit caching support
|
106
|
+
test_files:
|
107
|
+
- spec/geokit-cache_spec.rb
|
108
|
+
- spec/spec_helper.rb
|