module Enumerable
def hash_map
hash = {}
self.each {|o| hash.store(*yield(o))}
hash
end
end
my_hash = lines.hash_map {|l| l.split /:/}
Great suggestion on redit for creating a hash from other objects.
I added assert_empty to check that an array is empty.
But the line number from test helper.rb was the first line of the test helper, not the line in the test.
Instead, I wanted to see the actual failure as the first line. Welcome remove_me_from_trace(exception)
class Test::Unit::TestCase
def assert_empty(*args)
begin
args.unshift []
assert_equal *args
rescue => e
raise remove_me_from_trace(e)
end
end
def remove_me_from_trace(e)
x=e.backtrace.shift until (x =~ /test_helper.rb/ )
e
end
end
I normally don’t like hacking rails files directly, but this is the only way I could extend rake db:migrate to support a friendly VERSION.
railities/lib/tasks/database.rake on line 111.
# was ENV["VERSION"] ? ENV["VERSION"].to_i : nil
# looks up version based upon migration files
def version_num
version=ENV["VERSION"]
if version.to_i==0 && version.present?
matches=Dir["db/migrate/*#{ENV["VERSION"]}*"]
if matches.length > 0
#prune off directory name
matches=matches.collect{|n| n.gsub(/^.*\//,'')}
unless matches.length == 1
raise "VERSION #{version} matches "+
"#{matches.join(', ')}"
end
#prune off version num
version=matches.first.gsub(/([^_]*)_.*$/) {$1}
puts "VERSION=#{version}"
end
end
version ? version.to_i : nil
end
In 3 places (migrate, up, down), the version code is changed from
ENV["VERSION"] ? ENV["VERSION"].to_i : nil
to
version_num
Now rake db:migrate:down VERSION=create_photo works like a charm.
I have been using a schell script so I don’t need to remember the long version number associated with ruby migrations.
#given a migration task, return the version
#e.g.: rake db:migrate:redo VERSION=$(mver create_photo)
function mver() {
scr=${1:?Please specify migration action}
ls db/migrate/*${scr}* | sed 's=^[^0-9]*\([0-9][^_]*\)_.*$=\1='
}
which will run migration 20100302214328_create_photos_and_folders.rb