-
-
Save avdi/1516514 to your computer and use it in GitHub Desktop.
allowing objects to specify exception types
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
class Share | |
def call | |
list.each do |address| | |
referral = new_referral(address) | |
# All else being equal, I'd probably prefer a caller-specified | |
# fallback strategy | |
referral.save do | |
@unsaved << referral | |
end | |
end | |
end | |
def new_referral(address) | |
Referral.new(address) | |
end | |
end | |
class Referral | |
class Invalid < StandardError; end | |
def save | |
# ... | |
# if invalid: | |
if block_given? then yield else raise Invalid end | |
end | |
end |
Another approach is to accept an explicit :on_error option which takes a
callable.
…On Dec 24, 2011 10:01 AM, "Jim Gay" < ***@***.***> wrote:
Interesting approach. I've been playing with this.
It reads like the block should run during the save. Or in other words,
`save do` looks like something that happens during a successful save. But
this does nicely remove the explicit reference to the exception class.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1516514
Thanks for your thoughts on this!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting approach. I've been playing with this.
It reads like the block should run during the save. Or in other words,
save do
looks like something that happens during a successful save. But this does nicely remove the explicit reference to the exception class.