Class | Mack::Utils::Crypt::Keeper |
In: |
lib/mack-encryption/keeper.rb
|
Parent: | Object |
A singleton class that holds/manages all the workers for the system.
A worker must be defined as Mack::Utils::Crypt::<name>Worker and must define an encrypt(value) method and a decrypt(value) method.
Example:
class Mack::Utils::Crypt::ReverseWorker def encrypt(x) x.reverse end def decrypt(x) x.reverse end end
# File lib/mack-encryption/keeper.rb, line 22 22: def initialize 23: @crypt_workers_cache = {} 24: end
Returns a worker object to handle the encrytion/decryption. If the specified worker doesn‘t exist then Mack::Utils::Crypt::DefaultWorker is returned.
# File lib/mack-encryption/keeper.rb, line 29 29: def worker(key = :default) 30: worker = @crypt_workers_cache[key.to_sym] 31: if worker.nil? 32: worker_klass = key.to_s.camelcase + "Worker" 33: if Mack::Utils::Crypt.const_defined?(worker_klass) 34: worker = "Mack::Utils::Crypt::#{worker_klass}".constantize.new 35: else 36: worker = Mack::Utils::Crypt::DefaultWorker.new 37: end 38: @crypt_workers_cache[key.to_sym] = worker 39: end 40: worker 41: end