Skip to content

Commit

Permalink
Merge pull request #21 from ericcj/master
Browse files Browse the repository at this point in the history
objects aren't deep converted
  • Loading branch information
adelevie committed Aug 2, 2012
2 parents 5f0b57d + 97bf8ca commit 6063c89
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 36 deletions.
130 changes: 111 additions & 19 deletions lib/parse/datatypes.rb
Expand Up @@ -14,14 +14,26 @@ def initialize(data)
@parse_object_id = data[Protocol::KEY_OBJECT_ID]
end

def eql?(other)
self.class.equal?(other.class) &&
class_name == other.class_name &&
parse_object_id == other.parse_object_id
end

alias == eql?

def hash
class_name.hash ^ parse_object_id.hash
end

def as_json(*a)
{
Protocol::KEY_TYPE => Protocol::TYPE_POINTER,
Protocol::KEY_CLASS_NAME => @class_name,
Protocol::KEY_OBJECT_ID => @parse_object_id
}
end

def to_json(*a)
as_json.to_json(*a)
end
Expand All @@ -48,13 +60,36 @@ def initialize(data)
end
end

def eql?(other)
self.class.equal?(other.class) &&
value == other.value
end

alias == eql?

def hash
value.hash
end

def method_missing(method, *args, &block)
if value.respond_to?(method)
value.send(method, *args, &block)
else
super(method)
end
end

def respond_to?(method, include_private = false)
super || value.respond_to?(method, include_private)
end

def as_json(*a)
{
Protocol::KEY_TYPE => Protocol::TYPE_DATE,
"iso" => value.iso8601
}
end

def to_json(*a)
as_json.to_json(*a)
end
Expand All @@ -71,90 +106,147 @@ def initialize(data)
@value = Base64.decode64(bytes)
end

def eql?(other)
self.class.equal?(other.class) &&
value == other.value
end

alias == eql?

def hash
value.hash
end

def method_missing(method, *args, &block)
if value.respond_to?(method)
value.send(method, *args, &block)
else
super(method)
end
end

def respond_to?(method, include_private = false)
super || value.respond_to?(method, include_private)
end

def as_json(*a)
{
Protocol::KEY_TYPE => Protocol::TYPE_BYTES,
"base64" => Base64.encode64(@value)
}
end

def to_json(*a)
as_json.to_json(*a)
end
end

# Increment and Decrement
# ------------------------------------------------------------

class Increment
# '{"score": {"__op": "Increment", "amount": 1 } }'
attr_accessor :amount

def initialize(amount)
@amount = amount
end


def eql?(other)
self.class.equal?(other.class) &&
amount == other.amount
end

alias == eql?

def hash
amount.hash
end

def as_json(*a)
{
Protocol::KEY_OP => Protocol::KEY_INCREMENT,
Protocol::KEY_AMOUNT => @amount
}
end

def to_json(*a)
as_json.to_json(*a)
end
end

class Decrement
# '{"score": {"__op": "Decrement", "amount": 1 } }'
attr_accessor :amount

def initialize(amount)
@amount = amount
end


def eql?(other)
self.class.equal?(other.class) &&
amount == other.amount
end

alias == eql?

def hash
amount.hash
end

def as_json(*a)
{
Protocol::KEY_OP => Protocol::KEY_DECREMENT,
Protocol::KEY_AMOUNT => @amount
}
end

def to_json(*a)
as_json.to_json(*a)
end
end

# GeoPoint
# ------------------------------------------------------------

class GeoPoint
# '{"location": {"__type":"GeoPoint", "latitude":40.0, "longitude":-30.0}}'
attr_accessor :longitude, :latitude

def initialize(data)
@longitude = data["longitude"]
@latitude = data["latitude"]

if !@longitude && !@latitude
@longitude = data[:longitude]
@latitude = data[:latitude]
end
end


def eql?(other)
self.class.equal?(other.class) &&
longitude == other.longitude &&
latitude == other.latitude
end

alias == eql?

def hash
longitude.hash ^ latitude.hash
end

def as_json(*a)
{
Protocol::KEY_TYPE => Protocol::TYPE_GEOPOINT,
"latitude" => @latitude,
"longitude" => @longitude
}
end

def to_json(*a)
as_json.to_json(*a)
end
end


end
10 changes: 5 additions & 5 deletions lib/parse/util.rb
Expand Up @@ -9,9 +9,6 @@ def Parse.parse_json(class_name, obj)

if obj.nil?
nil
# String
elsif obj.is_a? String
parse_json class_name, JSON.parse(obj)

# Array
elsif obj.is_a? Array
Expand All @@ -25,10 +22,11 @@ def Parse.parse_json(class_name, obj)
parse_datatype obj
elsif obj.size == 1 && obj.has_key?(Protocol::KEY_RESULTS) && obj[Protocol::KEY_RESULTS].is_a?(Array)
obj[Protocol::KEY_RESULTS].collect { |o| parse_json(class_name, o) }
else # otherwise it must be a regular object
Parse::Object.new class_name, obj
else # otherwise it must be a regular object, so deep parse it avoiding re-JSON.parsing raw Strings
Parse::Object.new class_name, Hash[obj.map{|k,v| [k, parse_json(nil, v)]}]
end

# primitive
else
obj
end
Expand All @@ -44,6 +42,8 @@ def Parse.parse_datatype(obj)
Parse::Bytes.new obj
when Protocol::TYPE_DATE
Parse::Date.new obj
when Protocol::TYPE_GEOPOINT
Parse::GeoPoint.new obj
end
end
end
30 changes: 18 additions & 12 deletions test/test_datatypes.rb
Expand Up @@ -7,19 +7,19 @@ def test_pointer
Parse::Protocol::KEY_OBJECT_ID => "12345abcd"
}
p = Parse::Pointer.new data
assert_equal p.to_json, "{\"__type\":\"Pointer\",\"#{Parse::Protocol::KEY_CLASS_NAME}\":\"DatatypeTestClass\",\"#{Parse::Protocol::KEY_OBJECT_ID}\":\"12345abcd\"}"

assert_equal p.to_json, "{\"__type\":\"Pointer\",\"#{Parse::Protocol::KEY_CLASS_NAME}\":\"DatatypeTestClass\",\"#{Parse::Protocol::KEY_OBJECT_ID}\":\"12345abcd\"}"
end

def test_date
date_time = DateTime.now
data = date_time
parse_date = Parse::Date.new data

assert_equal parse_date.value, date_time
assert_equal JSON.parse(parse_date.to_json)["iso"], date_time.iso8601
end

def test_bytes
data = {
"base64" => Base64.encode64("testing bytes!")
Expand All @@ -30,31 +30,37 @@ def test_bytes
assert_equal JSON.parse(byte.to_json)[Parse::Protocol::KEY_TYPE], Parse::Protocol::TYPE_BYTES
assert_equal JSON.parse(byte.to_json)["base64"], Base64.encode64("testing bytes!")
end

def test_increment
amount = 5
increment = Parse::Increment.new amount

assert_equal increment.to_json, "{\"__op\":\"Increment\",\"amount\":#{amount}}"
end

def test_decrement
amount = 5
increment = Parse::Decrement.new amount

assert_equal increment.to_json, "{\"__op\":\"Decrement\",\"amount\":#{amount}}"
end

def test_geopoint
# '{"location": {"__type":"GeoPoint", "latitude":40.0, "longitude":-30.0}}'
data = {
"longitude" => 40.0,
"latitude" => -30.0
}
gp = Parse::GeoPoint.new data
assert_equal JSON.parse(gp.to_json)["longitude"], data["longitude"]

assert_equal JSON.parse(gp.to_json)["longitude"], data["longitude"]
assert_equal JSON.parse(gp.to_json)["latitude"], data["latitude"]
assert_equal JSON.parse(gp.to_json)[Parse::Protocol::KEY_TYPE], Parse::Protocol::TYPE_GEOPOINT

post = Parse::Object.new("Post")
post["location"] = gp
post.save
q = Parse.get("Post", post.id)
assert_equal gp, q["location"]
end
end
12 changes: 12 additions & 0 deletions test/test_object.rb
Expand Up @@ -64,4 +64,16 @@ def test_parse_delete
end
end

def test_deep_parse
other = Parse::Object.new "Post"
other.save
post = Parse::Object.new "Post"
post["other"] = other.pointer
post.save

q = Parse.get("Post", post.id)
assert_equal Parse::Pointer, q["other"].class
assert_equal other.pointer, q["other"]
end

end

0 comments on commit 6063c89

Please sign in to comment.