Project

chaingang

0.0
No commit activity in last 3 years
No release in over 3 years
A chainable API (like Arel) for ActiveResource.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.6.0
~> 2.4.0

Runtime

 Project Readme

Introduction

Chainable APIs are great. There's no denying it. With Rails 3, ActiveRecord got a chainable API. But not so for ActiveResource, that red-headed stepchild of the Rails 'verse.

Installation

It's a gem. Install it.

$ gem install chaingang

Usage

First, include ChainGang in your ActiveResource::Base derived client class.

class Article < ActiveResource::Base
  self.site = 'http://blah.com'
  include ChainGang
end

Then, go wild:

# find all the articles /articles/published.xml?author=moonmaster9000&genre=sci-fi
@articles = Article.find(:all).from(:published).where.author?("moonmaster9000").and.genre?("sci-fi")

# at this point, @articles hasn't actually made the network call yet. 
@articles.each do |article| # now it's made the network call
  puts article.title
end

# or, find a specific article by id, but add a "?preview_data=true" onto the query string.
@article = Article.find('some-article-id').where.preview_data?(true)

# or, find the first article by moonmaster9000 and get the title; /articles.xml?author=moonmaster9000
@article = Article.find(:first).where.author?("moonmaster9000").title

# to specifiy the reqeust format, you can use format() method, which temporarily set Article.format for this request.
@article = Article.find(:first).where.author?("moonmaster9000").format(:json)         # or :xml

Documentation

Checkout http://rdoc.info/gems/chaingang/frames