Created
August 28, 2008 19:42
-
-
Save adamwiggins/7805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateSemaphores < ActiveRecord::Migration | |
def self.up | |
create_table :semaphores do |t| | |
t.column :name, :text | |
t.column :value, :integer | |
end | |
add_index :semaphores, :name, :unique => true | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Semaphore.lock('lock1', 'lock2') do | |
# code | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Semaphore < ActiveRecord::Base | |
def self.lock(*keys) | |
transaction do | |
keys.sort.map { |key| create!(:name => key).destroy } | |
yield | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment