May 10

I liked Dr Nic’s github badge so much that I wrapped a sidebar plugin around it for Typo users. You can get it from github.

Or install it into typo like this:

# cd typo/vendor/plugins
# git clone git://github.com/peteonrails/githubsidebar.git github_sidebar
May 10

Dr. Nic has released a javascript based badge for GitHub. While I generally like to keep my sidebars clean because most readers just ignore the cruft in there, this badge is really useful for me. Plus, as the author points out, you get at least a 100% boost in coolness.

I like the badge, but it’s a little less than seamless looking in my sidebar. He’s made the source code available on github (of course), so I could download it and style it to my liking, but I’d much rather benefit from his future releases without having to re-download and merge.

Specifically, I’d like to be able to suppress the header the badge prints, so I can wrap it into a Typo plugin for a better look.

Working on a patch now to do just that.

May 08

I often have to iterate over a collection and perform some remote, or long running task on each member of the collection.

Threaded Collections is a package for iterating through collections over multiple threads. With large collections, sometimes it can be more efficient to process a collection in parallel, provided that the collected items don’t have a interdependencies, or need to be processed in a specific order.

Usage:

1
2
3
4
5
6
7
8
9
require "threaded_collections"
 
threadcount = 2
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
tps = ThreadedCollectionProcessor.new(arr)
tps.process(2) do |thread_id, item| 
  puts "Thread #{thread_id} processed item: #{item}" 
  sleep 1
end

I abstracted this pattern from a web services client that posted items from a collection, but
each request took a second to process. The remote service had plenty of threads available, so
I parallelized the task with this pattern.

I have no plans to break the interface but I do plan to make two major enhancements:

  1. Make it possible to mix this functionality in to the Ruby iterators, so you don’t have create the ThreadedCollectionProcessor.
  2. Make it work with fibers and processes in addition to threads.

If you want to check it out, you can get the source from the threadedcollections github site.

To install the gem without git:

1
bash# sudo gem install peteonrails-threaded-collections --source=http://gems.github.com
May 07

RESTful applications built with script/generate have a lot of repetitive code. Each generated controller will define default “index” “create” “new” “update” and “destroy” methods that looks similar from controller to controller.

There are at least FOUR plugins that aim to DRY up RESTful controllers each with their own benefits and weaknesses:

  1. make_resourceful - There’s a great Railscast on make_resourceful.
  2. resource_controller Provides a super-class controller that you inherit from. Pretty sweet at handling nested resources…..but don’t confuse it with…
  3. resources_controller — which uses a declarative syntax instead of a superclass. I like seeing “resources_controller_for :foos” in my code.
  4. resource_this — Heavily uses before_filters, if you’d prefer to see things that way.

I am working on a longer, more in-depth comparison of the available frameworks. but I’ve gotta tell you, a “winner” needs to shake out of this list. Having four very strong and useful, but VERY DIFFERENT, ways to DRY up controllers is not beneficial.

Do you remember when there were six ways to Paginate records in a Rails app? Now we use “will\_paginate”, and that’s how it’s done. Everyone knows how to use will_paginate, and it’s terse, well tested, and well supported.

So, which framework do you use? Let me know. I’m interested.

May 06

In my last post, I described an XML over HTTP service written in Java that I have to integrate with. I expressed a minor and general annoyance at the fact that the developers of this service specify that a “DateTime” parameter be specified in “milliseconds since the epoch”.

There is another typical problem with Java services that isn’t always as evident.

When Congress decides to change the Daylight Savings Time switch dates, the JVM typically needs to be patched separately from the OS it is running on. And this never happens.

So, if you are passing a message to a Java service gateway with a parameter like “delivery time” specified in “milliseconds since epoch”, and you think it’s Daylight Savings Time, but the Java service thinks it’s “Standard Time”, then your message will be delivered an hour late.

I usually deal with that case ad-hoc by specifying a variable in a YAML file, like this:

1
tz-fix: true

And then in my code like this:

1
2
3
4
  # Read in my config
  @@c = YAML.load_file(File.join(File.dirname(__FILE__), "myapp.yml"))   
# ...
  millis = millis - (60*60*1000) if @@c["tz-fix"]

If the remote service talks in “seconds since epoch”, I drop the “* 1000″.

Good luck.

May 06

Working in telecom, I have to integrate my Rails and plain Ruby apps with Java or Unix based systems running on remote hosts. Most often these systems are running remotely and are accessed via an XML-over-HTTP interface (no SOAP, no REST, blah…).

I’ve found that one of the most infuriating things that UNIX and Java “roll your own web service” writers do is specify “time” fields as “seconds since the epoch”. WORSE: Many Java service writers specify time fields as “milliseconds since the epoch”.

Come on, now, people? It’s a web service! Do you really think I care about accuracy in milliseconds?

I won’t go into a rant about this. Suffice it to say: I Find This BS To Be Lazy. It works that way so that the server software doesn’t have to do any complicated date parsing or conversions.

Fortunately, Ruby makes it easy to deal with if you know the right methods to use. Here’s how to convert to a UNIX or Java internal-time:

1
2
3
4
5
rubytime = Time.new  # => Tue May 06 11:11:05 -0400 2008
 
# .. Convert from Ruby time to Unix time. 
unixtime = rubytime.to_i            # 1210086665
javatime = rubytime.to_i * 1000  # 1210086665000

What about converting the other way? The example above are converting the “less precise” Ruby time (no milliseconds) into equally or more precise time formats. What do you do when you have a “milliseceonds since epoch” time and you want to use it in Ruby, but preserve the sub-second precision?

1
2
3
4
5
6
7
8
9
# Add just over a half second to the time used above
millis   = 1210086665555  
 
# adding .0 preserves subsecond accuracy
precise = Time.at(millis / 1000.0) 
 
precise.to_i            #  1210086665
precise.to_f            #  1210086665.555
precise.to_f * 1000  # 1210086665555.0

Now. Go convince all those legacy Java and Unix service writers to stop specifying “Epoch Time” in their interfaces! It’s Just Plain Lazy!

May 03

Alright. The Typo 5.x Standard Issue theme (which I really like, so much that I switched from Scribbish) does not account for the styles used by typo:code macros.

After much headache trying to get Typo to include the typocode themes in minimal.css while rendering themes (so that code would render nicely in any theme that I choose to use), I gave up. Really, I’m not sure I want to override typocode styles in every theme anyway, because someone might come up with an even better set of styles.

So instead, I’ve patched themes/standard_issue/stylesheets/application.css to include the typocode styles. As you can see, code is rendering much more bueno now.

Thanks Brent, for beating me up enough to invetigate a better theme.

If you use Standard Issue and want the updated stylesheet, grab it from my site.

Now, I am working on a bug in the ruby formatting that displays line numbers incorrectly, like this:

May 02

Code Highlighting in Typo is awful, cumbersome, and a pain. I am currently running this blog on Typo 5.0.3, and when I write an article using typo:code snippets using LivePreview, here is what I see:

But then it RENDERS like this, no matter which theme I am using:

GAH! That’s not what I thought I was getting! No friggin bueno! So I decided to try a third party code highlighter.

I really liked the TextMate Syntax Highlighter, but: it didn’t work either.

My friend and former colleague Brendan pointed me to a javascript-based code highlighter that does the formatting on the browser. That makes good sense to me. I like it. But it didn’t work either.

So, I’ve decided to roll up my sleeves and figure out why none of the included themes in Typo will render the code blocks the way I want. 2 hours later, I hate the CSS mess, so I’m spending my evening ripping the whole set of stylesheets apart to make them work right.

Stay tuned.

May 02

I host this blog with RailsPlayground. Mostly, I never have to edit files on my hosted accounts, because I use robust deployment tools. However, today I had to tweak my .htaccess file and found that my shared host didn’t have “emacs”.

Alright. Are you kidding me? No emacs? I know that most of the younguns these days use “vi” or something else to edit on UNIX. And that’s FINE. “vi” is small (it fits on a floppy!) powerful, and a generally great editor.

I simply come from the old school. When I was in college, someone asked my CS101 professor: “Mr. Anastasio, is there another UNIX editor I could use for my homework other than emacs?”

His reply: “No. If you are going to work in software, emacs is THE editor.”

So I fired off this message to tech support:

I am loving the service so far. Very easy to set up and use.

HOWEVER! I was surprised to see that you don’t have emacs installed on your developer accounts.

Any chance of getting it installed?
I can use vi and other editors, but I’m a developer who comes from the “emacs is the only way” camp. :)

Thanks!

I kid you not. TEN minutes later, I got this reply:

Hello Peter,

This is now installed on your server.

Note that your account is on a shared server, so if you try to copy very large
files it can use a lot of swap and memory space on the server. So if there are
any issues, we will be forced to disable it.

Aside from not understanding how I might copy large files with emacs and run the server out of RAM, I think this is a GREAT answer.

Thanks RailsPlayground!

If you are looking for a good Rails host, I suggest you read this article.