public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
100644 46 lines (43 sloc) 1.379 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Mack
  module Utils
    module Crypt
      # 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
      class Keeper
        include Singleton
      
        def initialize
          @crypt_workers_cache = {}
        end
        
        # Returns a worker object to handle the encrytion/decryption.
        # If the specified worker doesn't exist then Mack::Utils::Crypt::DefaultWorker
        # is returned.
        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
      
      end # Keeper
    end # Crypt
  end # Utils
end # Mack