Class: EzCrypter::Keeper
- Inherits:
-
Object
- Object
- EzCrypter::Keeper
- Includes:
- Singleton
- Defined in:
- lib/ezcrypter/keeper.rb
Overview
A singleton class that holds/manages all the workers for the system.
A worker must be defined as EzCrypter::<name>Worker and must define an ez_encrypt(value) method and a ez_decrypt(value) method.
Example:
class EzCrypter::ReverseWorker
def ez_encrypt(x)
x.reverse
end
def ez_decrypt(x)
x.reverse
end
end
Instance Method Summary collapse
-
#initialize ⇒ Keeper
constructor
A new instance of Keeper.
-
#worker(key = :default, options = {}) ⇒ Object
Returns a worker object to handle the encrytion/decryption.
Constructor Details
#initialize ⇒ Keeper
Returns a new instance of Keeper.
20 21 22 |
# File 'lib/ezcrypter/keeper.rb', line 20 def initialize @crypt_workers_cache = {} end |
Instance Method Details
#worker(key = :default, options = {}) ⇒ Object
Returns a worker object to handle the encrytion/decryption. If the specified worker doesn’t exist then EzCrypter::DefaultWorker is returned.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ezcrypter/keeper.rb', line 27 def worker(key = :default, = {}) # Needs poor man's camelcase if key.is_a?(Symbol) key = key.to_s key[0,1] = key[0,1].upcase end worker = @crypt_workers_cache[key.to_sym] if worker.nil? worker_klass = key + "Worker" if EzCrypter.const_defined?(worker_klass) worker = nil worker = eval("worker = EzCrypter::#{worker_klass}.new(options)") # puts "worker class inst #{worker.class.name}" else worker = EzCrypter::DefaultWorker.new() # puts "worker class inst defaulted to #{worker.class.name}" end @crypt_workers_cache[key.to_sym] = worker end worker end |