About
Parsistence provides an ActiveRecord/Persistence.js pattern to your Parse.com models on RubyMotion. It's an early fork from ParseModel by Alan deLevie but goes a different direction with its implementation.
Usage
Create a model:
class Post
include Parsistence::Model
fields :title, :body, :author
end
Create an instance:
p = Post.new
p.respond_to?(:title) #=> true
p.title = "Why RubyMotion Is Better Than Objective-C"
p. = "Josh Symonds"
p.body = "trololol"
p.saveEventually
Parsistence::Model
objects will respond_to?
to all methods available to PFObject
in the Parse iOS SDK, as well as 'fields' getters and setters. You can also access the PFObject
instance directly with, you guessed it, Parsistence::Model#PFObject
.
If you pass in a PFObject instance, it'll use that:
p = Post.new(pf_post)
If you pass in a hash, it'll set properties automatically:
p = Post.new(title: "Awesome")
p.title # => "Awesome"
Users
class User
include Parsistence::User
end
user = User.new
user.username = "adelevie"
user.email = "[email protected]"
user.password = "foobar"
user.signUp
users = User.all
users.map {|u| u.objectId}.include?(user.objectId) #=> true
Parsistence::User
delegates to PFUser
in a very similar fashion as Parsistence::Model
delegates to PFOBject
. Parsistence::User
includes Parsistence::Model
, in fact.
Queries
Queries use a somewhat different pattern than ActiveRecord but are relatively familiar. They are most like persistence.js.
Car.eq(license: "ABC-123", model: "Camry").order(year: :desc).limit(25).fetchAll do |cars, error|
if cars
cars.each do |car|
# `car` is an instance of your `Car` model here.
end
end
end
Chain multiple conditions together, even the same condition type multiple times, then run fetch
to execute the query. Pass in a block with two fields to receive the data.
Available Conditions
(note: each condition can take multiple comma-separated fields and values)
Method | Effect | Example |
---|---|---|
eq | Equal to |
|
notEq | NOT equal to |
|
gt | Greater than |
|
lt | Less than |
|
gte | Greater than or equal to |
|
lte | Less than or equal to |
|
order | Order by one or more fields (:asc/:desc). |
|
limit | Limit and offset. |
|
in | Get fields where value is contained in an array of values. |
|
Relationships
Define your relationships in the Parse.com dashboard and also in your models.
class Post
include Parsistence::Model
fields :title, :body, :author
belongs_to :author # Must be a "pointer" object on Parse.com
end
Author.where(name: "Jamon Holmgren").fetchOne do |fetchedAuthor, error|
p = Post.new
p.title = "Awesome Readme"
p.body = "Read this first!"
p. = fetchedAuthor
p.save
end
Installation
Either gem install Parsistence
then require 'Parsistence'
in your Rakefile
, OR
gem "Parsistence"
in your Gemfile. (Instructions for Bundler setup with Rubymotion)
Somewhere in your code, such as app/app_delegate.rb
set your API keys:
Parse.setApplicationId("1234567890", clientKey:"abcdefghijk")
To install the Parse iOS SDK in your RubyMotion project, read this and this.
License
See LICENSE.txt