Category Archives: Coding

Thoughts on, experiences with and byproducts of writing code.

Bash completion for Rake tasks

I have a poor memory, especially for things a computer should do for me. Frustrated with Rake commands (is it logs:clear or log:clear?) I turned to Bash completion. A web search found this basic implementation. Because it called rake -T on each invocation, it was unusably slow. Another search found Jonathan Palardy’s implementation with caching. Nice.

I worked on it a little more to be able to add it to Homebrew’s bash-completion package. Though it turns out that Homebrew’s package just grabs from the main bash-completion distribution so I’ll have to see about adding it there.

In any case, here in all its glory, a bash-completion for Rake. It caches automatically so all but the first use is fast. It also checks for a tmp/cache directory (present in Rails projects) and places the cache in there if available. This saves you from having to add the .rake_t_cache file to your SCM’s ignore list.

Posted in Coding | Comments closed

Show validation errors in RSpec be_valid output using a custom matcher

I’m getting into Rspec and loving it. So I was surprised when I get this pretty unhelpful message from a be_valid specification:

expected valid? to return true, got false

So I went looking for a way to see what errors there actually were in the validation. First I came upon rspec: model.should be_valid // opensoul.org. That looked good but it didn’t work for me on Rails 3 with Rspec 2. Then I found Creating a custom “be_valid” matcher for rspec_on_rails « nuby on rails, which has more details but still didn’t work. (Also the way I also found (My) RSpec best practices and tips | EggsOnBread, which is too good not to share.)

With some more searching I found a good walk-through on writing custom matchers RSpec 2 with an example of how to show errors on one specified field: solnic.eu / Custom RSpec-2 Matchers. I’m not sure how it’s different from the built-in errors_on. It also used the deprecated module Spec:

*****************************************************************
DEPRECATION WARNING: you are using a deprecated constant that will
be removed from a future version of RSpec.
<internal:lib/rubygems/custom_require>:29:in `require'
* Spec is deprecated.
* RSpec is the new top-level module in RSpec-2
***************************************************************

With the help of all the above, a version that works in Rails 3 and RSpec 2:

RSpec::Matchers.define :be_valid_verbose do
  match do |model|
    model.valid?
  end
  
  failure_message_for_should do |model|
    "#{model.class} expected to be valid but had errors:n #{model.errors.full_messages.join("n ")}"
  end

  failure_message_for_should_not do |model|
    "#{model.class} expected to have errors, but it did not"
  end

  description do
    "be valid"
  end
end

Change the name to “be_valid” if you want. That works but I don’t like replacing standard stuff. Guess I’m not a Rubyist yet.

Is there a way to get this functionality without writing a custom matcher? Is there a safer way to override the be_valid?

Posted in Coding | Tagged , | Comments closed

JavaScript code prettifiers

If you need a JavaScript code prettifier you’ve got basically two options, google-code-prettify and SyntaxHighlighter. I just tried them both and here’s what I found.

google-code-prettify SyntaxHighlighter
Support Google Code site. No wiki. Project maintainer Alex Gorbatchev who seems quite helpful.
Languages highlighted Lots. Lots.
Hosting Must download yourself. Can load URLs directly. (Donate if you use this.)
Branding None. Shows a help “?” which activates a dialog asking for a donation.

How do they look? I needed YAML highlighting so that’s what my screenshots are of. (Incidentally, the YAML “brush” is not distributed with SyntaxHighlighter.)

Here’s the Google one. It parses strings wrong so only some words are highlighted. It gets dates. It gets comments. It gets numbers but interprets them as numbers even when they’re part of a string.

So I tried Alex’s with the YAML brush. It’s a little better. It colors the keys of the hash but it misses the first of each hash, maybe because it’s in a list. It doesn’t detect strings or dates. It has the same problem with numbers.

But the killer is that it resized my <pre> out of the boundary. I tried hard-coding width but that had no effect. So I’ll go with the Google one. Maybe someone will improve it, or if I really need to I will.

Posted in Coding | Tagged | Comments closed

Consolidating multiple WordPress blogs using categories

I’ve had separate professional and personal blogs for a while because most people I know who would read one wouldn’t be interested in the other. Recently as I started getting back into writing code, I wanted to participate in the active community of blogging programmers. But start yet another blog? Instead I decided to make heavy use of categories in my primary WordPress blog to split the audiences. Here’s how I did it.
Read More »

Posted in Coding | Comments closed

FeedBurner feed per category

Summary: This post describes how to set up a WordPress blog with a FeedBurner feed per category. Basically, use this wp-feedburner plugin by unzipping the latest plugin archive into your wp-content/plugins folder.

Read More »

Posted in Coding | Comments closed

Learning in public

Years ago I was a full time web software developer, with the LAMP stack my sword and Eclipse key bindings in my muscle memory. Or something.

More recently I've been in a PhD program and my coding skills have gone rusty. Meanwhile, the landscape changed. Ruby? Mongo? Heroku? My research is now at a point that I'm developing a working system so I'm having to learn anew. Which can be fun. Especially that the web has made coding into such a social activity. Github: awesome. To do my part in the community that has helped me so much in learning the modern world of web development, I started this Tumblr to let me post the little personal discoveries I make along the way. So maybe it'll be that much easier for the next person.
And I hope to have fun while doing it.
Posted in Coding | Tagged | Comments closed

Friendster, FOAF, PLINK

When Friendster came out it was clearly only the tip of an iceberg of social software. It was also clear that Friendster could be no more because it wasn’t scalable. And people have learned not to become dependent on monopolies. I looked for an open-standards distributed alternative and found FOAF. It’s a framework, not an application. The only people to find in the network where zealots like myself. I dreamed of making something as easy to use as Friendster but using FOAF and other open technologies.

After a while of filling my head with FOAF and thinking about what to do with it, I realized that I wasn’t in the position to make something big. I could make some neat application of the network, but I couldn’t make the growth. Then TypePad showed up and it used FOAF to code the connections between members. Even the Dean campaign website used FOAF.

While I was on the FOAF development mailing lists, I learned of a project called PLINK (People Link) that did a lot of what I was planning on. I figured I’d wait and see how that went. Well, it’s here now and it’s pretty promising. You can browse a person and the FOAF files that were culled.

Of course, even with this growth it’s still far from the radar of Joe Netuser. Friendster’s still growing and Orkut is gaining a lot of attention.

What’s the use of all this? I’m not sure yet. But somehow, though perhaps perversely, it is fun. If you want to play, add me to your FOAF.

Posted in Coding | Comments closed
  • Subscribe

    Subscribe to Coding posts by feed or by email,
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 3 other subscribers