-
-
Save maxim/7cb390e4d1614ba0d3bd to your computer and use it in GitHub Desktop.
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
# the old way was md5 | |
class Foo | |
def digest | |
Digest::MD5.hexdigest(read_value) | |
end | |
end | |
# the new way is sha1 | |
class Foo | |
def digest | |
Digest::SHA1.hexdigest(read_value) | |
end | |
end | |
# you need to support both | |
class Foo | |
def digest | |
if old? | |
Digest::MD5.hexdigest(read_value) | |
else | |
Digest::SHA1.hexdigest(read_value) | |
end | |
end | |
end | |
# but you don't want these types of conditions scattered around your code | |
# you could do this: | |
class Foo | |
def digest | |
Digest::SHA1.hexdigest(read_value) | |
end | |
end | |
class FooCompat < Foo | |
def digest | |
if old? | |
Digest::MD5.hexdigest(read_value) | |
else | |
super | |
end | |
end | |
end | |
# and use FooCompat, then when migration finished - revert to Foo and delete FooCompat | |
# is there a better way? | |
# Ruby 2 solution | |
module CompatLayer | |
def digest | |
if old? | |
Digest::MD5.hexdigest(read_value) | |
else | |
super | |
end | |
end | |
end | |
class Foo | |
prepend CompatLayer # <-- just remove this line later and you're done | |
def digest | |
Digest::SHA1.hexdigest(read_value) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment