This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding recursion control to valid?(), save(), and changed?()
- Loading branch information
Showing
3 changed files
with
59 additions
and
6 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module RecursionControl | ||
class << self | ||
def stack | ||
@stack ||= {} | ||
end | ||
end | ||
|
||
# Shortcuts recursion by assuming a default return value. | ||
# | ||
# For example: consider what would happen if two associated | ||
# records each tried to validate the other. They would loop | ||
# recursively calling #valid? on the other until Ruby grew | ||
# tired and raised StackTooDeep. But in this situation, each | ||
# record is in fact valid because the other does in fact | ||
# exist. So by replacing recursive references with a default | ||
# value of true, we can remove the recursion without changing | ||
# the result. | ||
def without_recursion(method, default = true, &block) | ||
RecursionControl.stack[method] ||= [] | ||
|
||
return default if RecursionControl.stack[method].include? self | ||
RecursionControl.stack[method] << self | ||
result = yield | ||
RecursionControl.stack[method].delete(self) | ||
return result | ||
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