markbates / mack-more
- Source
- Commits
- Network (2)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
af4dac2
commit af4dac212e242b917ceaab5bec3233caa199295d
tree d7ba99a3e8763621f795a405a5b1a62f3db3a41f
parent 84155097ab040c81f2a281989ba2d23d539fce0a
tree d7ba99a3e8763621f795a405a5b1a62f3db3a41f
parent 84155097ab040c81f2a281989ba2d23d539fce0a
| af4dac21 » | markbates | 2008-08-12 | 1 | module Mack | |
| 2 | module Utils | ||||
| 3 | module Crypt | ||||
| 4 | # A singleton class that holds/manages all the workers for the system. | ||||
| 5 | # | ||||
| 6 | # A worker must be defined as Mack::Utils::Crypt::<name>Worker and must | ||||
| 7 | # define an encrypt(value) method and a decrypt(value) method. | ||||
| 8 | # | ||||
| 9 | # Example: | ||||
| 10 | # class Mack::Utils::Crypt::ReverseWorker | ||||
| 11 | # def encrypt(x) | ||||
| 12 | # x.reverse | ||||
| 13 | # end | ||||
| 14 | # | ||||
| 15 | # def decrypt(x) | ||||
| 16 | # x.reverse | ||||
| 17 | # end | ||||
| 18 | # end | ||||
| 19 | class Keeper | ||||
| 20 | include Singleton | ||||
| 21 | |||||
| 22 | def initialize | ||||
| 23 | @crypt_workers_cache = {} | ||||
| 24 | end | ||||
| 25 | |||||
| 26 | # Returns a worker object to handle the encrytion/decryption. | ||||
| 27 | # If the specified worker doesn't exist then Mack::Utils::Crypt::DefaultWorker | ||||
| 28 | # is returned. | ||||
| 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 | ||||
| 42 | |||||
| 43 | end # Keeper | ||||
| 44 | end # Crypt | ||||
| 45 | end # Utils | ||||
| 46 | end # Mack | ||||
