Skip to content

Instantly share code, notes, and snippets.

@saturnflyer
Created December 23, 2011 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saturnflyer/1515021 to your computer and use it in GitHub Desktop.
Save saturnflyer/1515021 to your computer and use it in GitHub Desktop.
allowing objects to specify exception types
class Share
def call
list.each do |address|
begin
referral = new_referral(address)
referral.save
rescue referral.invalid_exception_type
@unsaved << referral
end
end
end
def new_referral(address)
Referral.new(address)
end
end
class Referral
class Invalid < StandardError; end
def invalid_exception_type
Referral::Invalid
end
end
@saturnflyer
Copy link
Author

My goal with this is to reduce the references to external objects. There is only 1 reference to the Referral class from within Share. If that changes, the replacement merely needs to implement save and invalid_exception_type

@avdi
Copy link

avdi commented Dec 23, 2011

That's interesting. I will ponder this.

@avdi
Copy link

avdi commented Dec 24, 2011

See https://gist.github.com/1516514 for an alternative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment