geokit-cache 1.0.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/.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 +9 -0
- data/geokit-cache.gemspec +67 -0
- data/lib/geokit-cache.rb +2 -0
- data/lib/geokit/cache/active_record.rb +26 -0
- data/lib/geokit/cache/model.rb +117 -0
- data/spec/geokit-cache_spec.rb +289 -0
- data/spec/spec_helper.rb +9 -0
- metadata +110 -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.4
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class GeokitCacheGenerator < 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,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{geokit-cache}
|
8
|
+
s.version = "1.0.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Marcin Nowicki"]
|
12
|
+
s.date = %q{2009-10-06}
|
13
|
+
s.description = %q{Caching support in database for locations geocoded by andre/geokit-gem}
|
14
|
+
s.email = %q{pr0d1r2@ragnarson.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"generators/geokit_cache/geokit_cache_generator.rb",
|
27
|
+
"generators/geokit_cache/templates/model.rb",
|
28
|
+
"generators/geokit_cache/templates/model_spec.rb",
|
29
|
+
"geokit-cache.gemspec",
|
30
|
+
"lib/geokit-cache.rb",
|
31
|
+
"lib/geokit/cache/active_record.rb",
|
32
|
+
"lib/geokit/cache/model.rb",
|
33
|
+
"spec/geokit-cache_spec.rb",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/Pr0d1r2/geokit-cache}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.5}
|
40
|
+
s.summary = %q{Geokit caching support}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/geokit-cache_spec.rb",
|
43
|
+
"spec/spec_helper.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<Pr0d1r2-active_record_connectionless>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<Pr0d1r2-active_record_geocodable>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<htmlentities>, [">= 0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
57
|
+
s.add_dependency(%q<Pr0d1r2-active_record_connectionless>, [">= 0"])
|
58
|
+
s.add_dependency(%q<Pr0d1r2-active_record_geocodable>, [">= 0"])
|
59
|
+
s.add_dependency(%q<htmlentities>, [">= 0"])
|
60
|
+
end
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
63
|
+
s.add_dependency(%q<Pr0d1r2-active_record_connectionless>, [">= 0"])
|
64
|
+
s.add_dependency(%q<Pr0d1r2-active_record_geocodable>, [">= 0"])
|
65
|
+
s.add_dependency(%q<htmlentities>, [">= 0"])
|
66
|
+
end
|
67
|
+
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,117 @@
|
|
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
|
+
before_save :make_complete_address_prepared
|
29
|
+
is_geocodable :require => true
|
30
|
+
base.extend(ClassMethods)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def cache!(attributes)
|
35
|
+
self.attributes = attributes
|
36
|
+
save if new_record? || changed?
|
37
|
+
end
|
38
|
+
|
39
|
+
def set_instance_variables_from_geo!
|
40
|
+
GEO_ATTRIBUTES.each do |geo_attribute|
|
41
|
+
instance_variable_set(geo_attribute, geo.send(geo_attribute))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def update_action!
|
46
|
+
set_instance_variables_from_geo!
|
47
|
+
save if changed?
|
48
|
+
end
|
49
|
+
|
50
|
+
def needs_update?
|
51
|
+
!by_google? && geocoding_successful?
|
52
|
+
end
|
53
|
+
|
54
|
+
def update!
|
55
|
+
update_action! if needs_update?
|
56
|
+
end
|
57
|
+
|
58
|
+
def update_and_return!
|
59
|
+
update!
|
60
|
+
geoloc
|
61
|
+
end
|
62
|
+
|
63
|
+
def fake_geoloc
|
64
|
+
geoloc = Geokit::GeoLoc.new
|
65
|
+
GEO_ATTRIBUTES.each do |geo_attribute|
|
66
|
+
geoloc.instance_variable_set(geo_attribute, instance_variable_get(geo_attribute))
|
67
|
+
end
|
68
|
+
geoloc.success = success?
|
69
|
+
geoloc
|
70
|
+
end
|
71
|
+
|
72
|
+
def successful_geoloc
|
73
|
+
geo if geocoding_successful?
|
74
|
+
end
|
75
|
+
|
76
|
+
def geoloc
|
77
|
+
successful_geoloc || fake_geoloc
|
78
|
+
end
|
79
|
+
|
80
|
+
def by_google?
|
81
|
+
provider == 'google'
|
82
|
+
end
|
83
|
+
|
84
|
+
def changed_to_google?
|
85
|
+
by_google? && provider_changed?
|
86
|
+
end
|
87
|
+
|
88
|
+
def changed?
|
89
|
+
lat_changed? || lng_changed? || changed_to_google?
|
90
|
+
end
|
91
|
+
|
92
|
+
def decode_html_entities_in_text_attributes
|
93
|
+
TEXT_ATTRIBUTES.each {|a| self.send("#{a}=", HTMLEntities.new.decode(self.send(a)))} if geocoding_successful?
|
94
|
+
end
|
95
|
+
|
96
|
+
def make_complete_address_prepared
|
97
|
+
self.complete_address = self.class.prepare_complete_address(complete_address)
|
98
|
+
end
|
99
|
+
|
100
|
+
module ClassMethods
|
101
|
+
def geocode(complete_address)
|
102
|
+
record = find_or_create(complete_address)
|
103
|
+
record.update_and_return!
|
104
|
+
end
|
105
|
+
|
106
|
+
def find_or_create_by_complete_address(complete_address)
|
107
|
+
find_by_complete_address(prepare_complete_address(complete_address)) || new(:complete_address => prepare_complete_address(complete_address))
|
108
|
+
end
|
109
|
+
|
110
|
+
def prepare_complete_address(complete_address)
|
111
|
+
complete_address.downcase.strip.split(',').reject(&:blank?).collect(&:strip).join(', ')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,289 @@
|
|
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
|
+
describe 'decode_html_entities_in_text_attributes' do
|
216
|
+
it 'should decode html-entities in all text attributes when geocoding was successful' do
|
217
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(true)
|
218
|
+
GeokitCache::TEXT_ATTRIBUTES.each do |text_attribute|
|
219
|
+
@geokit_cache.should_receive(:send).with(text_attribute).and_return(text_attribute)
|
220
|
+
@geokit_cache.should_receive(:send).with("#{text_attribute}=", text_attribute.to_s)
|
221
|
+
end
|
222
|
+
@geokit_cache.decode_html_entities_in_text_attributes
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'should not decode html-entities in any text attributes when geocoding was not successful' do
|
226
|
+
@geokit_cache.should_receive(:geocoding_successful?).and_return(false)
|
227
|
+
GeokitCache::TEXT_ATTRIBUTES.each do |text_attribute|
|
228
|
+
@geokit_cache.should_not_receive(:send).with(text_attribute)
|
229
|
+
@geokit_cache.should_not_receive(:send).with("#{text_attribute}=", text_attribute.to_s)
|
230
|
+
end
|
231
|
+
@geokit_cache.decode_html_entities_in_text_attributes
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'make_complete_address_prepared should use class prepare_complete_address' do
|
236
|
+
@geokit_cache.complete_address = 'Complete Address'
|
237
|
+
GeokitCache.should_receive(:prepare_complete_address).with('Complete Address').and_return('complete address')
|
238
|
+
@geokit_cache.make_complete_address_prepared.should == 'complete address'
|
239
|
+
end
|
240
|
+
|
241
|
+
describe 'class method' do
|
242
|
+
it 'geocode should return geoloc for updated or new record' do
|
243
|
+
record = mock(GeokitCache)
|
244
|
+
record.should_receive(:update_and_return!)
|
245
|
+
GeokitCache.should_receive(:find_or_create).with(:complete_address).and_return(record)
|
246
|
+
GeokitCache.geocode(:complete_address)
|
247
|
+
end
|
248
|
+
|
249
|
+
describe 'find_or_create_by_complete_address' do
|
250
|
+
it 'should return existing record' do
|
251
|
+
GeokitCache.should_receive(:find_by_complete_address).with('complete_address').and_return(:record)
|
252
|
+
GeokitCache.find_or_create_by_complete_address('Complete_Address').should == :record
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'should create new record when record does not exist in database' do
|
256
|
+
GeokitCache.should_receive(:find_by_complete_address).with('complete_address')
|
257
|
+
GeokitCache.should_receive(:new).with(:complete_address => 'complete_address').and_return(:new_record)
|
258
|
+
GeokitCache.find_or_create_by_complete_address('Complete_Address').should == :new_record
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe 'prepare_complete_address' do
|
263
|
+
it 'should make complete_address downcase' do
|
264
|
+
GeokitCache.prepare_complete_address('Complete Address').should == 'complete address'
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'should strip leading and trailing spaces' do
|
268
|
+
GeokitCache.prepare_complete_address("\n\t complete address \n\t").should == 'complete address'
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'should properly strip colons' do
|
272
|
+
GeokitCache.prepare_complete_address('complete, address,country').should == 'complete, address, country'
|
273
|
+
GeokitCache.prepare_complete_address('complete,, address,,country').should == 'complete, address, country'
|
274
|
+
GeokitCache.prepare_complete_address('complete, , address, ,country').should == 'complete, address, country'
|
275
|
+
GeokitCache.prepare_complete_address("complete, \t, address, \t,country").should == 'complete, address, country'
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
|
283
|
+
describe Example do
|
284
|
+
|
285
|
+
it 'geocoder should be GeokitCache' do
|
286
|
+
Example.new.geocoder.should == GeokitCache
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geokit-cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Nowicki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-06 00:00:00 +02: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
|
+
- geokit-cache.gemspec
|
75
|
+
- lib/geokit-cache.rb
|
76
|
+
- lib/geokit/cache/active_record.rb
|
77
|
+
- lib/geokit/cache/model.rb
|
78
|
+
- spec/geokit-cache_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/Pr0d1r2/geokit-cache
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options:
|
86
|
+
- --charset=UTF-8
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: "0"
|
94
|
+
version:
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: "0"
|
100
|
+
version:
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.5
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Geokit caching support
|
108
|
+
test_files:
|
109
|
+
- spec/geokit-cache_spec.rb
|
110
|
+
- spec/spec_helper.rb
|