Skip to content

Instantly share code, notes, and snippets.

@maxim

maxim/compat.rb Secret

Last active December 20, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxim/7cb390e4d1614ba0d3bd to your computer and use it in GitHub Desktop.
Save maxim/7cb390e4d1614ba0d3bd to your computer and use it in GitHub Desktop.
# 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