Ruby DSL Trick with the Hash
Today I had to rig up a method for defining symlinks in Capistrano. I never really liked how the ln command worked to begin with; I think of symlinks in terms of:
Alias A targets File B
To make this easier I wrote up this method:
def link(link)
source, target = link.keys.first, link.values.first
run "ln -nfs #{target} #{source}"
end
Which reads as:
link "alias_a" => "file_b"
I love finding these little ways that make my code more readable.




Comments 3 Comments
def copy(files)
files.each do |from, to|
file(to, File.read(File.join(@template_path, from)))
end
end
copy 'layout.html.erb' => 'app/views/layouts/application.html.erb'
Very nifty.
def link(link)
source, target = *link.first
run "ln -nfs #{target} #{source}"
end