Created
April 24, 2009 22:01
-
-
Save julesfern/101365 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module DataMapper::RepositoryLock | |
| # Places a repository_lock method on the including class, which can | |
| # be executed given a repository name as an argument. | |
| def self.append_features(base) | |
| super | |
| base.extend(ClassMethods) | |
| end | |
| module ClassMethods | |
| # Locks the model to the given named repository. | |
| def repository_lock(repo) | |
| extend DataMapper::RepositoryLock::SingletonMethods | |
| include DataMapper::RepositoryLock::InstanceMethods | |
| cattr_accessor :lock_to_repository | |
| self.lock_to_repository = repo | |
| end | |
| end | |
| module SingletonMethods | |
| # The following methods are intended to be a stable, repository-safe frontend | |
| # that rescue you from from having to put repository blocks absolutely fucking | |
| # everywhere in the application. | |
| # Override the finder methods to always use the @@lock_to_repository_name repository regardless of the | |
| # contents of DataMapper's repository context stack. | |
| def first(*args); DataMapper.repository(lock_to_repository) { super }; end | |
| def all(*args); DataMapper.repository(lock_to_repository) { super }; end | |
| def count(*args); DataMapper.repository(lock_to_repository) { super }; end | |
| # Also sets the default repository name, which you may override locally. | |
| def default_repository_name; lock_to_repository; end | |
| end | |
| module InstanceMethods | |
| # Overrides the #save method to save all objects to the @@lock_to_repository_name repostory regardless | |
| # of the contents of DataMapper's repository context stack. | |
| def save(*args); DataMapper.repository(self.class.lock_to_repository) { super }; end | |
| def valid?(*args); DataMapper.repository(self.class.lock_to_repository) { super }; end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment