1 2 3 4 5 6 7 8 9
Neat, eh? By the way, you can check it out over at JSFiddle for a live demo!
Recently I created a framework called Socks, a web framework written in Ruby.
One of the useful features I wanted to implement for Socks was plugins - plugins you could design easily with a handy DSL.
After a while of tinkering on RubyFiddle, I finally got what I was looking for:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
How did I come to make this pretty nice looking plugin DSL? Let’s Take a look at
Socks::Plugin, the container of the plugin source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Note: You can have multiple actions and call them one by one, since an action has a name you just name them different things each action.
Hope you learned something from this DSL source, because I certainly did!
As you may have seen in my last post, the gem
http_router provides a amazing way to interact with a nice router in a Rack app. This time, I’m going to show you an example of testing
http_router located in a
config/router.rb file from the spec directory. For these tests, I will be using RSpec, the simple Ruby testing DSL that is my personal favorite:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Many web-Ruby programmers that love to get into the core of Ruby and Rack may have troubles designing a routing interface for your application/framework.
Luckily, there’s a simple, Sinatra-like way to do this using
Just like any gem, you just use:
In the config.ru
To hook up the router to your application you’ll need to require an
http_router file, or run a new
http_router inside the
config.ru. For now, I’ll jot down the simple way to do it:
1 2 3 4 5 6 7
As you can see, the
config.ru is really no different than a regular
config.ru, besides the use of running
http_router rather than a regular class for your app.
This way to make an app is great, and it almost looks like Sinatra, except for one thing, what if you wan’t your routes to be in another file, like say,
config/router.rb. Sounds like Rails, good enough:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
And there we go! Define your routes in an external file, and run the router via a class with a constant. Now you’ve got a working, routed app, ready to be used for the web.
Welcome to Hot Chocolate, a place where I show you pretty much anything interesting I find, learn, or is on my mind.