Module: Geokit::Cache::Model

Defined in:
lib/geokit/cache/model.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

GEO_ATTRIBUTES =
[
  :provider,
  :lng,
  :lat,
  :full_address,
  :state,
  :success,
  :accuracy,
  :city,
  :country_code,
  :precision,
  :street_address,
  :zip
]
TEXT_ATTRIBUTES =
[:city, :state, :full_address, :street_address]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/geokit/cache/model.rb', line 25

def self.included(base)
  base.class_eval do
    before_save :decode_html_entities_in_text_attributes
    before_save :make_complete_address_prepared
    is_geocodable :require => true
    base.extend(ClassMethods)
  end
end

Instance Method Details

#by_google?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/geokit/cache/model.rb', line 80

def by_google?
  provider == 'google'
end

#cache!(attributes) ⇒ Object



34
35
36
37
# File 'lib/geokit/cache/model.rb', line 34

def cache!(attributes)
  self.attributes = attributes
  save if new_record? || changed?
end

#changed?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/geokit/cache/model.rb', line 88

def changed?
  lat_changed? || lng_changed? || changed_to_google?
end

#changed_to_google?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/geokit/cache/model.rb', line 84

def changed_to_google?
  by_google? && provider_changed?
end

#decode_html_entities_in_text_attributesObject



92
93
94
# File 'lib/geokit/cache/model.rb', line 92

def decode_html_entities_in_text_attributes
  TEXT_ATTRIBUTES.each {|a| self.send("#{a}=", HTMLEntities.new.decode(self.send(a)))} if geocoding_successful?
end

#fake_geolocObject



63
64
65
66
67
68
69
70
# File 'lib/geokit/cache/model.rb', line 63

def fake_geoloc
  geoloc = Geokit::GeoLoc.new
  GEO_ATTRIBUTES.each do |geo_attribute|
    geoloc.instance_variable_set(geo_attribute, instance_variable_get(geo_attribute))
  end
  geoloc.success = success?
  geoloc
end

#geolocObject



76
77
78
# File 'lib/geokit/cache/model.rb', line 76

def geoloc
  successful_geoloc || fake_geoloc
end

#make_complete_address_preparedObject



96
97
98
# File 'lib/geokit/cache/model.rb', line 96

def make_complete_address_prepared
  self.complete_address = self.class.prepare_complete_address(complete_address)
end

#needs_update?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/geokit/cache/model.rb', line 50

def needs_update?
  !by_google? && geocoding_successful?
end

#set_instance_variables_from_geo!Object



39
40
41
42
43
# File 'lib/geokit/cache/model.rb', line 39

def set_instance_variables_from_geo!
  GEO_ATTRIBUTES.each do |geo_attribute|
    instance_variable_set(geo_attribute, geo.send(geo_attribute))
  end
end

#successful_geolocObject



72
73
74
# File 'lib/geokit/cache/model.rb', line 72

def successful_geoloc
  geo if geocoding_successful?
end

#update!Object



54
55
56
# File 'lib/geokit/cache/model.rb', line 54

def update!
  update_action! if needs_update?
end

#update_action!Object



45
46
47
48
# File 'lib/geokit/cache/model.rb', line 45

def update_action!
  set_instance_variables_from_geo!
  save if changed?
end

#update_and_return!Object



58
59
60
61
# File 'lib/geokit/cache/model.rb', line 58

def update_and_return!
  update!
  geoloc
end