Evaluating alternative Decorator implementations in Ruby

Evaluating alternative Decorator implementations in Ruby:

Recently, decorators have become a big part of my Ruby on Rails life.

We used them heavily in a recent client project, Harold Giménez wrote a great post about them, Avdi Grimm is writing about them in Objects on Rails, and Jeff Casimir has a great presentation about them.

Until recently, I still had some questions, however, such as:

Should I roll my own decorators?
If I roll my own, what are the tradeoffs of different implementations?
Do I care about the “transparent interface” requirements of the Gang of Four’s decorator definition?
Is it good or bad that the decorated object’s class is the decorator instead of the component?

[Good explanations.]
Source:

Simple RedisToGo connection

I did this many times before, but it keeps cropping up (I’m not at all sure why.) Here it is…

require 'rubygems'
require 'uri'
require 'redis'

#RedisToGo gives you the "redis://" uri.
uri = URI.parse('redis://username:password@host:1234/')
unless uri.nil?
  p uri.host
  p uri.port
  p uri.password
  p '---'
  r = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :thread_safe => true)
  p r.info
end

A Gentle Introduction to CarrierWave

Let’s see this modularity first hand by building up a CarrierWave uploader from scratch.

To begin with, we’ll install CarrierWave:

  gem install carrierwave

Then, we can make the world’s shortest uploader:

  require 'carrierwave'

  class MyUploader < CarrierWave::Uploader::Base
    storage :file
  end

Even at this point, we can start saving files:

  file = File.open('example.jpg')
  uploader = MyUploader.new
  uploader.store!(file)

[snip -ed]

If you’re ready for more, check out:

I’m sure you’ll enjoy using CarrierWave as much as I have!

[Nice.]
Source: Union Station

Busy Developers Guide: CoffeeScript

Please note that these instructions are for busy developers, and assume that you are one. If so, this should help, if you’re not, these may not. Sorry about that. Below are the steps I used…

  1. First get npm (the node package manager).
  2. You’ll find a one line install such as this or similar: curl http://npmjs.org/install.sh | sh
  3. Next CoffeeScript: npm install -g coffee-script (Leave off the -g if you don’t wish to install globally.)
  4. Assuming that went well you should be able to type “coffee” on the command line and see a "coffee>" prompt.
  5. Pat yourself on the back and start developing! But wait, a little more structure might help… You can skip the steps below and check out the repository.
  6. Create a new folder and get it set up as a repository. For me that looks something like this: mkdir ~/code/learning_coffeescript; cd ~/code/learning_coffeescript; git init;
  7. Create some files a bit of structure. A top level public directory and src directory. Inside public I created a lib dir and an index.html file.
  8. The index.html file contains a base html 5 template, and in my learning case it included a line pointing to the hosted jquery.js lib, and our newly compiled javascript file (yeah, I know, patience… I’ll get to it in a minute.)
  9. Back in the terminal, at your project root, type coffee -o public/lib -cw src which will compile all the CoffeScript files written in src into javascript in lib. It is “watching” the src files timestamps so if you update and save a file it will recompile.
  10. Assuming you’ve got everything wired up correctly, you can fire up a browser, open the index.html file and go to town.
  11. If you want to get snazzy you can rackup file_server.ru -p 1111 and the project will run under a basic Rack file server.
  12. If you wish to be all hip and edgy you can download and install Pow, symlink your local clone of the repository and develop to your hearts content.

Feel free to make pull requests, or send me issues, updates, or notes. I’m sure I have a lot to learn about CoffeeScript how best to integrate it with jQuery, etc. In the end though, I’m enjoying the syntax. It was just not clear how to get a project going. Next I’ll be looking at testing…

The week in links (09/20)

The week in links (06/28)

More interesting this week was writing my first Rails3 app. An internal helper app, something that makes our lives just a touch easier. It’s the sort of app that 37Signals would turn into a revenue stream. There’s one more feature I’d like to see added but the core is there and working well.

blurred_artifact.jpg