Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing ActiveRecord-like associations for an API wrapper

I recently wrote ParseResource, which is a Ruby API wrapper for Parse.com's REST api.

Here's a some basic usage:

class Post < ParseResource
  fields :title, :author, :body
end
p = Post.create(:title => "Hello world", :author => "Alan", :body => "ipso lorem")

The project is fairly young, and a feature I really want to implement is associations. Something like this:

class Author < ParseResource
  has_many :posts
  fields :name, :email
end
class Post < ParseResource
  belongs_to :author
  fields :title, :body
end
a = Author.create(:name => "Alan", :email => "[email protected]")
p = Post.create(:title => "Associated!", :body => "ipso lorem", :author => a)
p.author.class #=> Author
p.author.name #=> "Alan"
a.posts #=> an array of Post objects

I'd love any advice, pointers, and pitfalls from anyone who has implemented something similar as well as from anyone who has a grasp of Parse's REST API.

like image 801
user94154 Avatar asked Oct 09 '11 22:10

user94154


1 Answers

I've found using DataMapper ( http://datamapper.org ) it's pretty easy to get it working with almost any datastore. You can write an adapter that talks to your datastore and then use all of the power of DataMapper directly as if your data was in SQL. Here's a link that explains a bit about writing one of these adapters. http://www.killswitchcollective.com/articles/55_datamapperabstractadapter_101

like image 151
Ben Avatar answered Nov 15 '22 18:11

Ben