Created
July 14, 2011 08:21
-
-
Save paneq/1082096 to your computer and use it in GitHub Desktop.
Which version do you prefere?
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
require 'test/unit' | |
class KeyTest < Test::Unit::TestCase | |
def test_key | |
object = Object.new | |
assert_equal(:submit, object_key(object)) | |
def object.persisted?; true end | |
assert_equal(:update, object_key(object)) | |
def object.persisted?; false end | |
assert_equal(:create, object_key(object)) | |
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
def object_key | |
if @object && @object.respond_to?(:persisted?) | |
@object.persisted? ? :update : :create | |
else | |
:submit | |
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
def object_key | |
return :submit unless @object && @object.respond_to?(:persisted?) | |
return @object.persisted? ? :update : :create | |
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
def object_key | |
# assume that nil does not respond to :persisted? and if it does, then respect that | |
return :submit unless @object.respond_to?(:persisted?) | |
return @object.persisted? ? :update : :create | |
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
def object_key | |
if @object && @object.respond_to?(:persisted?) && @object.persisted? | |
:update | |
elsif @object && @object.respond_to?(:persisted?) | |
:create | |
else | |
:submit | |
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
def object_key | |
return :submit if ! persistable?(@object) | |
return :update if ! @object.persisted? | |
return :create if @object.persisted? | |
end | |
def persistable?(object) | |
object && object.respond_to?(:persisted?) | |
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
def object_key | |
return :submit unless @object.respond_to?(:persisted?) | |
return :update if @object.persisted? | |
return :create | |
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
persisted = @object.respond_to?(:persisted?) ? @object.persisted? : nil | |
case persisted | |
when true then :update | |
when false then :create | |
else :submit | |
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
def object_key(object) | |
object.persisted? ? :update : :create | |
rescue; :submit | |
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
def object_key(object) | |
object.persisted? ? :update : :create rescue NoMethodError :submit | |
end |
I am not following the 'ifs on the client side, do u have an example?
Exceptions are good to go all the way up the call stack, is that what u mean?
Piotr
…On 15 lip 2011, at 13:28, ***@***.*** wrote:
Premature optimization...
I have seen this flow argument many times - why exactly exceptions can't be used when controlling the flow?
The example above is not the best one as it catches NoMethodError which is a bit extrem, but sometimes it's nice to raise ProductNotAvailable. If you're not using exceptions then you end up with lots of ifs on the client side. I prefer the "Tell, Don't Ask" approach.
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1082096
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Premature optimization...
I have seen this flow argument many times - why exactly exceptions can't be used when controlling the flow?
The example above is not the best one as it catches NoMethodError which is a bit extrem, but sometimes it's nice to raise ProductNotAvailable. If you're not using exceptions then you end up with lots of ifs on the client side. I prefer the "Tell, Don't Ask" approach.