A nice wrapper for your Parse models on RubyMotion, complete with querying and much more.
Ruby
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
lib
spec
vendor
.gitignore
Gemfile
Parsistence.gemspec
README.md
Rakefile

README.md

Parse.com is shutting down.

Due to this sad announcement, we will no longer support Parsistence. You're free to continue to use it as long as you wish, and perhaps Parsistence can be modified to work with any self-hosted Parse Server instance, but that will be up to the community.

Jamon Holmgren January 28, 2016

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.author = "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 = "adelevie@gmail.com"
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
Tree.eq(name: "Fir").fetchAll do |trees|
  ...
end
notEq NOT equal to
Tree.notEq(name: "Fir").fetchAll do |trees|
  ...
end
gt Greater than
Tree.gt(height: 10).fetchAll do |trees|
  ...
end
lt Less than
Tree.lt(height: 10).fetchAll do |trees|
  ...
end
gte Greater than or equal to
Tree.gte(height: 10).fetchAll do |trees|
  ...
end
lte Less than or equal to
Tree.lte(height: 10).fetchAll do |trees|
  ...
end
order Order by one or more fields (:asc/:desc).
Tree.order(height: :asc).fetchAll do |trees|
  ...
end
limit Limit and offset.
Tree.limit(25, 10).fetchAll do |trees|
  ...
end
in Get fields where value is contained in an array of values.
Tree.in(height: [10, 15, 20, 25]).fetchAll do |trees|
  ...
end

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.author = 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.

Testing

You will need to install the Parse SDK to run the tests. Installation is done via Cocoapods.

bundle
rake pod:install

You must also provide the Parse App ID and Client Key as environment variables when running the tests.

PARSE_APPLICATION_ID="yourappid" PARSE_CLIENT_KEY="yourclientkey" rake spec

License

See LICENSE.txt