Skip to content

Commit

Permalink
Merge pull request #143 from RobotsandRockets/convenience
Browse files Browse the repository at this point in the history
Convenience Methods
  • Loading branch information
adelevie committed Oct 7, 2014
2 parents c59b6fb + 3baf583 commit 7898bf9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/parse/model.rb
@@ -1,8 +1,14 @@
# -*- encoding : utf-8 -*-
module Parse
class Model < Parse::Object
def initialize
super(self.class.to_s)

def initialize(data=nil)
super(self.class.to_s,data)
end

def self.find(object_id)
self.new Parse::Query.new(self.to_s).eq('objectId',object_id).first
end

end
end
5 changes: 5 additions & 0 deletions lib/parse/object.rb
Expand Up @@ -48,6 +48,11 @@ def new?
self["objectId"].nil?
end

def update_attributes(data={})
data.each_pair { |k,v| self[k] = v }
save
end

# Write the current state of the local object to the API.
# If the object has never been saved before, this will create
# a new object, otherwise it will update the existing stored object.
Expand Down
13 changes: 12 additions & 1 deletion lib/parse/query.rb
Expand Up @@ -42,7 +42,13 @@ def or(query)
self
end

def eq(field, value)
def eq(hash_or_field,value=nil)
return eq_pair(hash_or_field,value) unless hash_or_field.is_a?(Hash)
hash_or_field.each_pair { |k,v| eq_pair k, v }
self
end

def eq_pair(field, value)
add_constraint field, Parse.pointerize_value(value)
self
end
Expand Down Expand Up @@ -116,6 +122,11 @@ def where_as_json
end
end

def first
self.limit = 1
get.first
end

def get
uri = Protocol.class_uri @class_name
if @class_name == Parse::Protocol::CLASS_USER
Expand Down

0 comments on commit 7898bf9

Please sign in to comment.