Class: Mack::Utils::Crypt::Keeper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mack-encryption/keeper.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeKeeper

Returns a new instance of Keeper.



22
23
24
# File 'lib/mack-encryption/keeper.rb', line 22

def initialize
  @crypt_workers_cache = {}
end

Instance Method Details

#worker(key = :default) ⇒ Object

Returns a worker object to handle the encrytion/decryption. If the specified worker doesn’t exist then Mack::Utils::Crypt::DefaultWorker is returned.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mack-encryption/keeper.rb', line 29

def worker(key = :default)
  worker = @crypt_workers_cache[key.to_sym]
  if worker.nil?
    worker_klass = key.to_s.camelcase + "Worker"
    if Mack::Utils::Crypt.const_defined?(worker_klass)
      worker = "Mack::Utils::Crypt::#{worker_klass}".constantize.new
    else
      worker = Mack::Utils::Crypt::DefaultWorker.new
    end
    @crypt_workers_cache[key.to_sym] = worker
  end
  worker
end