hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Thursday

2006.02.23

Wonder of the When-Be-Splat #

by why in bits

Well, okay, yes, we already know Ruby is expressive. And, if I know you, you’ll see a drop of awesomeness in:

 BOARD_MEMBERS = ['Jan', 'Julie', 'Archie', 'Stewick']
 HISTORIANS = ['Braith', 'Dewey', 'Eduardo']

 case name
 when *BOARD_MEMBERS
   "You're on the board!  A congratulations is in order." 
 when *HISTORIANS
   "You are busy chronicling every deft play." 
 end

I was refactoring a parser and it occured to me that I could just do a when *tokens.keys at one point. Ruby treats it just like a list of conditions. Consider Olympic ice dancing out-graced!

Addendum: I’m just gonna log a few of the other discoveries implied here, just to prove how handily this waxes if..include?’s sorry snout.

 case name when "Arthur", *BOARD_MEMBERS
   "Either you are a board member... or you are Arthur." 
 end

 case name when *BOARD_MEMBERS|HISTORIANS
   "We welcome you all to the First International
    Symposium of Board Members and Historians Alike." 
 end

What's in Camping 1.4? #

by why in inspect

Okay, you can try out Camping 1.3.69 from gems. Camping is a microframework. Railsish aesthetics for wee CGIs.

 gem install camping --source code.whytheluckystiff.net

Sessioning

To get sessions working for your application:

  1. require 'camping/session'
  2. In your application’s create method, add a call to Camping::Models::Schema.create_schema
  3. Throughout your application, use the @state var like a hash to store your application’s data.

More notes about this at CampingSessions on the wiki.

Helpers#URL

Builds a complete URL to a controller route or a path, returning a URI object.

Assuming the Hoodwink.d app is mounted at http://localhost:3301/hoodwinkd/, in a controller or view you’ll see the following results:

  >> URL()
  => (URI:http://localhost:3301/hoodwinkd/)
  >> self.URL
  => (URI:http://localhost:3301/hoodwinkd/)
  >> URL(Static, 'js/prototype.js')
  => (URI:http://localhost:3301/hoodwinkd/static/js/prototype.js)
  >> URL("/boingboing.net/setup")
  => (URI:http://localhost:3301/hoodwinkd/boingboing.net/setup)
  >> URL("http://google.com")
  => (URI:http://google.com/)

Find it also in the new docs.

Service Overrides

Adding stuff like sessioning and authentication demands hooks on the controller. Rails uses hooks like before_filter to do this. Camping doesn’t have bytes to spare to make this happen, but things have been re-stacked to let you slide in new service methods which intercept all controller calls.

 class LoginError < Exception; end
 module ClownsOnly
   def service(*a)
     unless @state.occupation == :clown
       raise LoginError, "For clown's eyes only." 
     end
     super(*a)
   end
 end

This service method can be slid into your application’s top module like so:

 Camping.goes :Blog

 module Blog
   include ClownsOnly
 end

Fully explained at Before and After Overrides on the wiki.

YARV No Longer an Acronym for "You Acknowledge the Reek of Vapors" #

by why in inspect

Good thing gabriele renzi is out there, because I’ve been off my shoe this week. Funny story: he was out reading blogs and he stumbled acrosst a real blog’s post. Namely, Minero Aoki has Rails running on YARV 0.4.0.

 /var/www/rails/testsite % /usr/local/pkg/yarv/bin/ruby script/server
 => Booting WEBrick...
 => Rails application started on http://0.0.0.0:3000
 => Ctrl-C to shutdown server; call with --help for options
 [2006-02-20 10:40:23] INFO  WEBrick 1.3.1
 [2006-02-20 10:40:23] INFO  ruby 1.9.0 (2006-02-14) [x86_64-linux]
 [2006-02-20 10:40:23] INFO  WEBrick::HTTPServer#start: pid=25032 port=3000

This is just like that feeling you get when you look up in the sky and a legion of manskunks is carrying your uncle away. Apocalycious. (gabriele’s blog is right here.)

To be clear: Like Chris Williams says, this isn’t like stable or anything. It’s just one of those, you know, hey, wow, YARV is really spreading its wings here.