Skip to content

Instantly share code, notes, and snippets.

@tmiller
Last active December 16, 2015 00:39
Show Gist options
  • Save tmiller/5349102 to your computer and use it in GitHub Desktop.
Save tmiller/5349102 to your computer and use it in GitHub Desktop.
# lib/cryptography_adapter.rb
# Parent adapter class
class CryptographyAdapter
def encrypted?(data)
raise "Method not implmented"
end
def decrypt(data)
raise "Method not implmented"
end
end
# config/initializers/cryptography.rb
Cryptography::cryptography_klass = CryptographyAdapter::GPG
# lib/cryptography.rb
# Public interface for your encryption and decryption needs
class Cryptography
cattr_accessor :cryptography_klass
attr_reader :crypto
def initialize()
@crypto = self.class.cryptography_klass.new
end
def encrypted?(data)
@crypto.encrypted?(data)
end
def decrypt(data)
@crypto.decrypt(data)
end
end
# lib/cryptography_adapter/gpg.rb
# Used in production
class CryptographyAdapter::GPG < CryptographyAdapter
def encrypted?(data)
# Return true for encrypted data
end
def decrypt(data)
# Decrypt some data
end
end
# lib/cryptography_adapter/rot13.rb
# Used in tests
class CryptographyAdapter::ROT13 < CryptographyAdapter
def encrypted?(data)
# Return true for encrypted data
end
def decrypt(data)
# Decrypt some data
end
end
# test/test_helper.rb
Cryptography::cryptography_klass = CryptographyAdapter::ROT13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment