Permalink
Browse files

Merge pull request #48 from mattvv/master

GeoPoint Support
  • Loading branch information...
2 parents 0332bed + 71942f4 commit cb371c026462fce372c1937efa96f902dbac6012 @adelevie committed Nov 3, 2012
Showing with 91 additions and 0 deletions.
  1. +26 −0 README.md
  2. +3 −0 lib/parse_resource/base.rb
  3. +19 −0 lib/parse_resource/types/parse_geopoint.rb
  4. +43 −0 test/test_types.rb
View
26 README.md
@@ -203,6 +203,32 @@ end
If you want to use parse_resource to back a simple authentication system for a Rails app, follow this [tutorial](http://asciicasts.com/episodes/250-authentication-from-scratch), and make some simple modifications.
+GeoPoints
+
+```ruby
+ class Place < ParseResource::Base
+ fields :location
+ end
+
+ place = Place.new
+ place.location = ParseGeoPoint.new :latitude => 34.09300844216167, :longitude => -118.3780094460731
+ place.save
+ place.location.inspect #=> #<ParseGeoPoint:0x007fb4f39c7de0 @latitude=34.09300844216167, @longitude=-118.3780094460731>
+
+
+ place = Place.new
+ place.location = ParseGeoPoint.new
+ place.location.latitude = 34.09300844216167
+ place.location.longitude = -118.3780094460731
+ place.save
+ place.location.inspect #=> #<ParseGeoPoint:0x007fb4f39c7de0 @latitude=34.09300844216167, @longitude=-118.3780094460731>
+
+ server_place = Place.find(place.objectId)
+ server_place.location.inspect #=> #<ParseGeoPoint:0x007fb4f39c7de0 @latitude=34.09300844216167, @longitude=-118.3780094460731>
+ server_place.location.latitude #=> 34.09300844216167
+ server_place.location.longitude #=> -118.3780094460731
+```
+
DEPRECATED
Associations
View
3 lib/parse_resource/base.rb
@@ -9,6 +9,7 @@
require "parse_resource/query_methods"
require "parse_resource/parse_error"
require "parse_resource/parse_exceptions"
+require "parse_resource/types/parse_geopoint"
module ParseResource
@@ -396,6 +397,8 @@ def get_attribute(k)
result = DateTime.parse(attrs[k]["iso"])
when "File"
result = attrs[k]["url"]
+ when "GeoPoint"
+ result = ParseGeoPoint.new(attrs[k])
end #todo: support other types https://www.parse.com/docs/rest#objects-types
else
result = attrs["#{k}"]
View
19 lib/parse_resource/types/parse_geopoint.rb
@@ -0,0 +1,19 @@
+class ParseGeoPoint
+ attr_accessor :latitude, :longitude
+
+ def initialize(hash=nil)
+ if hash.nil?
+ self.latitude=0.0
+ self.longitude=0.0
+ else
+ self.latitude = hash["latitude"] || hash[:latitude]
+ self.longitude = hash["longitude"] || hash[:longitude]
+ end
+
+ end
+
+ def to_pointer
+ {"__type"=>"GeoPoint", :latitude=> self.latitude, :longitude => self.longitude}
+ end
+
+end
View
43 test/test_types.rb
@@ -0,0 +1,43 @@
+require 'helper'
+require 'parse_resource'
+
+ParseResource::Base.load!(ENV["PARSE_RESOURCE_APPLICATION_ID"], ENV["PARSE_RESOURCE_MASTER_KEY"])
+
+class Place < ParseResource::Base
+ fields :name, :location
+end
+
+class TestParseResource < Test::Unit::TestCase
+
+ def test_saving_geopoint_with_coords
+ Place.destroy_all
+ place = Place.new
+ place.name = "Office"
+ place.location = ParseGeoPoint.new
+ place.location.latitude = 34.09300844216167
+ place.location.longitude = -118.3780094460731
+ place.save
+ assert_equal Place.count, 1
+ end
+
+ def test_saving_geo_point_with_quick_init
+ Place.destroy_all
+ place = Place.new
+ place.location = ParseGeoPoint.new :latitude => 34.09300844216167, :longitude => -118.3780094460731
+ place.save
+ assert_equal Place.count, 1
+ end
+
+ def test_fetching_geopoint_field
+ Place.destroy_all
+ place = Place.new
+ place.location = ParseGeoPoint.new :latitude => 34.09300844216167, :longitude => -118.3780094460731
+ place.save
+ assert_equal Place.count, 1
+
+ server_place = Place.find(place.objectId)
+ assert_equal server_place.location.latitude, 34.09300844216167
+ assert_equal server_place.location.longitude, -118.3780094460731
+ end
+
+end

0 comments on commit cb371c0

Please sign in to comment.