Skip to content

Instantly share code, notes, and snippets.

@harrisj
Created January 16, 2014 21:11
Show Gist options
  • Save harrisj/8463502 to your computer and use it in GitHub Desktop.
Save harrisj/8463502 to your computer and use it in GitHub Desktop.
Here's an example of doing yields in Ruby. The fail! and finished! methods set the state of the load in the DB and save the execution time
def self.audit(command = 'Load')
unless block_given?
raise ArgumentError, "You can only invoke audit with a block"
end
a = nil
begin
a = Load.create :command => command, :status => 'started', :start_time => Time.now
yield a
# rescue ActiveRecord::ActiveRecordError
# raise
rescue Exception => e
a.fail!(e)
end
a.finished!
a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment