public
Description: Think resource_controller, except for sinatra.
Homepage:
Clone URL: git://github.com/giraffesoft/classy_resources.git
name age message
file .gitignore Wed Jan 28 08:45:16 -0800 2009 ignore pkg [giraffesoft]
file LICENSE Tue Jan 27 13:22:23 -0800 2009 Initial commit to classy_resources. [giraffesoft]
file README.rdoc Mon Feb 16 13:31:57 -0800 2009 update README [giraffesoft]
file Rakefile Mon Feb 16 13:31:53 -0800 2009 add sinatra dep [giraffesoft]
file VERSION.yml Mon Feb 16 13:41:02 -0800 2009 Version bump to 0.3.1 [giraffesoft]
file classy_resources.gemspec Mon Feb 16 13:41:04 -0800 2009 Regenerated gemspec for version 0.3.1 [giraffesoft]
directory lib/ Mon Feb 16 13:15:22 -0800 2009 require active_support [giraffesoft]
directory test/ Mon Feb 16 13:10:54 -0800 2009 require activerecord 2.2.2 [giraffesoft]
README.rdoc

Classy Resources

With a simple, declarative syntax, you can create active_resource compatible REST APIs incredibly quickly.

Think resource_controller, except for Sinatra.

Installation

  sudo gem install giraffesoft-classy_resources

Usage

  require 'rubygems'
  require 'sinatra'
  require 'classy_resources'
  require 'classy_resources/active_record'
  # ... or require 'classy_resources/sequel'
  # more ORMs coming (it's also easy to implement your own)...

  define_resource :posts, :member     => [:get, :put, :delete],
                          :collection => [:get, :post],
                          :formats    => [:xml, :json, :yaml]

The above declaration will create the five actions specified, each responding to all of the formats listed.

  - GET /resources.format      # => index
  - POST /resources.format     # => create
  - GET /resources/1.format    # => show
  - PUT /resources/1.format    # => update
  - DELETE /resources/1.format # => destroy

Since ClassyResources was designed to be active resource compatible, the params formats and return values are what AR expects.

Overrides

In the above example, :posts would map to a Post class. If your class is named differently, you just override class_for. For example, if your Post class was stored in a module:

  def class_for(resource)
    MyModule.const_get(resource.to_s.singularize.classify.constantize)
  end

Or, if you wanted to change how objects were being serialized:

  def serialize(object, format)
    MySerializer.new(object, format).to_s
  end

Other method signatures you might want to override:

  - def load_collection(resource)
  - def build_object(resource, params)
  - def load_object(resource, id)
  - def update_object(object, params) # Note that this doesn't save. It just changes the attributes.
  - def destroy_object(object)

Copyright

Copyright © 2008 James Golick, Daniel Haran, GiraffeSoft Inc.. See LICENSE for details.