Skip to content

Commit

Permalink
Merge pull request #48 from mattvv/master
Browse files Browse the repository at this point in the history
GeoPoint Support
  • Loading branch information
adelevie committed Nov 3, 2012
2 parents 0332bed + 71942f4 commit cb371c0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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. 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 DEPRECATED
Associations Associations


Expand Down
3 changes: 3 additions & 0 deletions lib/parse_resource/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "parse_resource/query_methods" require "parse_resource/query_methods"
require "parse_resource/parse_error" require "parse_resource/parse_error"
require "parse_resource/parse_exceptions" require "parse_resource/parse_exceptions"
require "parse_resource/types/parse_geopoint"


module ParseResource module ParseResource


Expand Down Expand Up @@ -396,6 +397,8 @@ def get_attribute(k)
result = DateTime.parse(attrs[k]["iso"]) result = DateTime.parse(attrs[k]["iso"])
when "File" when "File"
result = attrs[k]["url"] result = attrs[k]["url"]
when "GeoPoint"
result = ParseGeoPoint.new(attrs[k])
end #todo: support other types https://www.parse.com/docs/rest#objects-types end #todo: support other types https://www.parse.com/docs/rest#objects-types
else else
result = attrs["#{k}"] result = attrs["#{k}"]
Expand Down
19 changes: 19 additions & 0 deletions lib/parse_resource/types/parse_geopoint.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions test/test_types.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.