RestfulX Framework and Merb 1.0+

dima edited this page Aug 14, 2010 · 3 revisions

Getting up and running with the RestfulX framework and Merb

What you’ll need:

  1. Merb 1.0 (with ActiveRecord or DataMapper set-up)
  2. Flex SDK 3.0
  3. MySQL 5.0+

Before you start you might want to add Flex SDK bin folder to your $PATH variable if you haven’t already. This will allow you to invoke commands such as mxmlc from the command line and run rake tasks such as rx:flex:build and rx:air:build.

  • On OS X it’s typically /Applications/Adobe Flex Builder 3/sdks/3.0.0/bin
  • On Win32 it’s C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\bin

1. Do:

$>sudo gem install restfulx
$>merb-gen app pomodo
$>cd pomodo

2. Now edit config/dependencies.rb (pomodo is the new merb application we’ve just generated):

  • add dependency "restfulx"

3. Then run:

$>rx-gen .
$>./script/generate rx_config

4. Then create a file called db/model.yml:

$>touch db/model.yml

Edit db/model.yml to contain the following:

project:
 - name: string
 - notes: text
 - start_date: date
 - end_date: date
 - completed: boolean
 - belongs_to: [user]
 - has_many: [tasks]

location:
 - name: string
 - notes: text
 - belongs_to: [user]
 - has_many: [tasks]

task:
 - name: string
 - notes: text
 - start_time: datetime
 - end_time: datetime
 - completed: boolean
 - next_action: boolean
 - belongs_to: [project, location, user]

note:
 - content: text
 - belongs_to: [user]

user:
 - login: string
 - first_name: string
 - last_name: string
 - email: string
 - has_many: [tasks, projects, locations]
 - has_one: [note]

You are now ready to develop your Merb + RestfulX application using either ActiveRecord or Data Mapper. If using Data Mapper make sure to include dm-serializer in your dependencies.

5. You can generate all your flex code using:

$>./script/generate rx_yaml_scaffold

No attempt is made to set-up Merb routes, controllers, models, etc. These will vary depending on the OR mapping tool you use and your coding style. As a Merb developer you are expected to create these yourself :).

You can message FXML or JSON from your controllers. Add provides :fxml or provides :json to your controllers to get either format set-up.

Remember to return the model you just created/updated or destroyed. This is part of the service provider contract. Refer to: RestfulX XML-over-HTTP provider API for details. You can also use Working with RestfulX Models for an example of what you can do with the framework.

6. When you want to build the Flex app, run:

$>rake rx:flex:build

7. Finally, run merb:

$>merb

OK, we can now navigate to http://localhost:4000 and checkout our newly created Flex app talking to Merb!

You can use the same rx-gen tools to create your Sinatra app (or whatever else) too! You are just expected to do your server-side coding by youself, no pre-baked generators here.

While code generation is a quick way to get started, it falls far short of exposing you to all the things you can do with the framework. Examine the code you just generated and refer to Working with RestfulX Models for more information on what you can accomplish.

For more information on the RestfulX framework refer to RestfulX Framework Wiki.

Want a more feature complete example? Check out Pomodo On Rails