July 11, 2007

Posted by John

Tagged clogging and time

Older: Hash#except

Newer: Customizing Logger

Clogging: Code Blogging

Most likely I will be flamed for introducing such a moronic name or this post will be ignored but trust me, it’s worth a read. Anyway…there are a few blogs of late that have had simple but cool ideas. Revolution on Rails has started a code digest (#1 and #2) and Pivotal Blabs posts tumble style ‘Interesting Things’ and ‘Ask for Help’.

What I like is typically each post from these authors is short, quickly digestible and straight up code. Heck, sometimes it’s only a few lines:

def double_rendering_action
  render :template => "wrangler/monkey" if wrangle_monkey?
  render :template => "wrangler/sleep" unless performed?
end

The Pressure

Dang, who new performed? was even a method? I think, too often, I feel as though I have to provide a full write up of every little snippet or library that I post about here. I know it will take me an hour to write it, edit it and post some cool screen captures so often I put it off until eventually I forget about it.

The truth is that when I read blogs, I typically scan for code. Who has time to read a long post? With over 400 subs in google reader, I know I don’t. From now on, I’m going to post more of a mix. Some articles will be full length and explain concepts in more detail while others might be three lines of code or several snippets with a few comments.

Clogging

clogging woman

We have blogging, micro-blogging, and tumbling so why not start clogging (code blogging). Clogging is to blogging what RailsCasts are to Peepcode, each have their place.

Baby’s first clog

Heck, I’ll even get the ball rolling here in this post:

module Addicted
  module Chronicable
    
    def self.included(base)
      base.extend ClassMethods
    end
    
    module ClassMethods
      def chronicable(*attrs)
        attrs.each do |attr|
          define_method("#{attr}_string") do
            send(attr) ? send(attr).to_formatted_s(:long) : ''
          end

          define_method("#{attr}_string=") do |new_start_at_string|
            # chronic does not like commas as of 0.2.3 so i gsub them out
            new_start_at_string = new_start_at_string.gsub(',', '') unless new_start_at_string.nil?
            self.send("#{attr}=", Chronic.parse(new_start_at_string))
          end
        end
      end
    end
  end
end

ActiveRecord::Base.send :include, Addicted::Chronicable

# Then in your model
class Task < ActiveRecord::Base
  chronicable :due_at
end

# And in your view
<%= f.text_field :due_at_string %>

# now the Task due_at datetime is auto parsed by chronic
# only text pastie here: http://pastie.caboo.se/77902.txt

When a problem comes along, you must clog it

That was easy, eh? Next time you learn a new method or figure something schweeeet out, clog it before you forget it.

11 Comments

  1. Haha, that’s awesome — the picture adds a nice visual touch as well. I guess “clogging” beats “boding” at least, too.

  2. Sites like http://snippets.dzone.com provide RSS feeds based on tags. You dump a snippet/code, and then tag it.

    I personally subscribe to “ruby”, “rails”, “javascript” tag feeds there.

  3. Damn you, I now have Devo stuck in my head! chronicable is pretty smooth. I second Dr Nic’s recommendation of Snippets. If only they had support for favorite snippets….

  4. What is this piece of code supposed to do?

  5. I’m pretty much pimping myself here, but I’ve been doing a blog involving little else. Check the link…

  6. @drnic – Yep, I have an account but I have to agree with newland. The lack of favorites and particular social aspects is a killer. Pastie needs accounts. I’d love to get my hands on the pastie source. I’m actually toying with creating an app where you have an account and can mark pasties as favorites to help keep all you snippets in one place, but who has time, eh?

    @al – It creates an easy way to use chronic to parse datetime fields by creating virtual attributes for the dates.

    @Greg – Awesome. Subscribed.

  7. Hmm.. interesting idea, but the term “Clog” is already taken for a ‘Comment Log’.. Sorry Bud! ;-)

  8. @Amr – What is a comment log?

  9. This may just be my newb-ness, but I cant get this to work. I create a file called addicted.rb in my lib directory, but when ever I put chronicable :field_name I get a method not found error. Where’d I goof?

  10. Ha, I know where the photo of that clog was taken!

  11. Brian – the line below needs to be in environment.rb I think.

    ActiveRecord::Base.send :include, Addicted::Chronicable

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.