ruby

Posted on October 09, 2008 to Rails and ruby  

In several apps I've worked on, we've wanted a way to create a unique string. For example, users cannot have duplicate email addresses. So when creating many users via the Factory pattern, email addresses inevitably end up being something like "barney#{rand(1000)}@foo.org".

This always leads to problems. UUIDs can be handy but then you have a 30 character long email address.

Why not just a global, app-level incrementor? (utilizing the Singleton pattern)

It will always output a unique integer, starting with the number 1. Multiple objects can use this, so long as they do not require "random" integers to be in a row, but rather simply unique.

Enough talk. Here's the code:


module Incrementor

def self.next
@counter ||= Incrementor::Counter.new
@counter.next
end

class Counter
def initialize
@i = 0
end
def next
@i += 1
end
end

end

In rails, you can throw that as 'incrementor.rb' in your config/initializers folder.

Usage:


=> Incrementor.next
=> 1
=> Incrementor.next
=> 2
...

Posted on (not yet posted) to ruby and go  

Go is a simple framework to share and distribute command-line utils with yourself (on other machines), friends, coworkers and … whomever might find them useful.

Getting Started

First install the go gem with:

sudo gem sources -a http://gems.github.com
sudo gem install sbraford-go

Next “setup” go (build your ~/.go directory) with:

go setup

See the list of go commands:

go

(when not passed any options, it simply shows a list of available commands)

Ok – nothing special so far. Let’s say your friend has an html2haml script that you need to use. While one script here and there wouldn’t be such a big deal, after a while it’d be nice to have an easier way to manage these types of utilities.

Managing your Go Plugins via Git

You now have a “~/.go” directory which we are going to turn into a publicly shared Git repository. If any Go Plugins require sensitive information, these should be placed in ~/.goconfig or environment variables. (or you can simply make a private git repository)

After you’ve created a new Git repository, leave the instructional page open. We’ll simply use an existing directory and add it to the remote repo, for example like this one .

Let’s get started:

cd ~/.go
git add .
git commit -m ‘first commit’
git remote add origin <your repo’s git url>
git push origin master

Now let’s add that recursive html2haml script. Go needs a raw file, so on this page we select the raw output and pass this link to go install:


go install http://github.com/sbraford/go-plugins/tree/master%2Fhtml2haml_recursive.rb?raw=true

You should see:
Installing plugin: Html2hamlRecursive
Html2hamlRecursive Plugin installed to: /Users/foo/.go/html2haml_recursive.rb

Now just add this to git and commit/push:

git add .
git commit -a -m “added recursive html2haml go plugin”
git push

Sharing Across Machines

Once you have your Go plugins directory stored at git, it’s trivial to clone this to other machines.

Simply cone your go plugin repository to ~/.go and you’re set.