Archives for November 2008

Rendering synthetic JSON

Rails has a neat to_json method you can use for output JSON, so you can quite easily do something like this:

format.json do
  render :json => @contents.to_json( :only => [:id,:title] )
end

and Rails will dutifully output the right thing for you.

In my case though we're building an API for integrating with some 3rd party hardware and we wanted to put some synthetic attributes into the JSON to make it easier for them. How to do this?

Adding attributes to the model didn't seem appropriate since the information was related to this integration in this controller. We could have added singleton methods to the model instances but that looked ugly. Writing a playlist.json.erb file was another possibility but it sucked to need to do that when all I really wanted to do was say:

@contents.to_json( :synthetic => lambda { |record| gen_synth_attr( record ) } )

DHH was suggesting to use a decorator class which gets the job done:

@contents.collect { |m| Decorator.new(m) }

So I was in for defining my own serializer anyway, I felt if it was worth doing that it must be worth trying to make it nicer.

Here's what I've ended up with:

Rather than monkey patching I include Reeplay::ExtendedSerialization into my models that I use this way. Here's an example use:

This lets me duck defining a template or polluting my model for such simple extension.

27/11/2008 15:18 by Matt Mower | Permalink | comments:
More about:

Channels I am hanging around in

On the philosophy of "if you build they will come" I am hanging around in #midi and #logicstudio. I've seen a few folks drop by #midi over the weeks but usually when I am asleep and they don't hang around. I've more hope for #logicstudio in that there are now 3 of us camping out there. If you are a logic user then get over #logicstudio and join us!

25/11/2008 11:41 by Matt Mower | Permalink | comments:
More about:

Thank you Pragmatic Programmers

I just cashed in the 35%-off coupon the Pragmatic Programmers gave me to celebrate their fifth birthday. The coupons ranged from 10%-60% off, I guess you must have ordered quite a lot to get the 60% coupon :)

I think they are a model for how an online publishing company should operate. They offer quick access via their PDF approach, use a lot of feedback in the development of their books, offer book & PDF deals, don't use DRM (even on their video). Also their account management site that lets you see all your orders and generate new PDF's is very effective.

More power to them. I look forward to my 'Happy 10th Birthday' coupon and will be expecting it to be a good deal closer to 60%!

10/11/2008 13:45 by Matt Mower | Permalink | comments:

277

"This week the Information Commissioner revealed that the number of data breaches - including lost laptops and memory sticks containing sensitive personal records - had risen to 277 since the loss of 25million child benefit records was disclosed nearly a year ago." -- [source: Daily Mail]

Do you need any more proof that we should not be trusting the government to build a huge database about us?

04/11/2008 08:39 by Matt Mower | Permalink | comments:

All systems nearly go

My hobby project, Elysium, is getting closer to being something you can really play with.

One area that has really come along lately is support for scripting. You can now attach ruby callbacks to things like notes & generators, create MIDI triggers (that run ruby scripts in response to MIDI CC messages from hardware controllers or other applications), and also create timers that run scripts at regular intervals. Here's a taste of how it looks inside the app:

Script Inspector
Uploaded with plasq's Skitch!

This panel exposes 8 flags, 8 continuous variables, and 4 timers. The flags & variables can be referenced by scripts but also manipulated directly through the UI. This allows you to interact with your scripts as well as offer feedback. You could, for example, use one of the sliders with appropriate min and max values to represent the energy level of your piece that could control how often generators run. The possibilities are endless.

Since it includes quite a lot of scripting using MacRuby I'm hoping to maybe release a v0.8 as a public beta in time for RubyConf next week. Not sure if I'll have the time, lots to do at work this week, but it could be fun to try.

03/11/2008 00:08 by Matt Mower | Permalink | comments:

Probabilistic monome step sequencer

Quite a mouthful, but mattly got his work-in-progress Ruby step sequencer working with one of his monomes last night and the effect is very cool:


Ouroubourus Monome Step Sequencer Demo from matt lylon on Vimeo.

There's going to be an interesting mix of Ruby & MIDI hackers at this years RubyConf. Wish I could be there.

02/11/2008 12:02 by Matt Mower | Permalink | comments:
More about: