Class: Smith::AclTypeCache
- Inherits:
-
Object
- Object
- Smith::AclTypeCache
- Includes:
- MurmurHash3, Singleton
- Defined in:
- lib/smith/messaging/acl_type_cache.rb
Constant Summary collapse
- SUPPORTED_FORMATS =
[:string, :binary]
- DEFAULT_FORMAT =
:string
Instance Method Summary collapse
-
#add(type) ⇒ true|false
Add the type to the cashe.
-
#clear! ⇒ Object
Clear the internal hashes.
-
#dump_hashes(format = DEFAULT_FORMAT) ⇒ Hash
Dump the hashes hash.
-
#dump_types(format = DEFAULT_FORMAT) ⇒ Hash
Dump the type hash.
-
#get_by_hash(type, format = DEFAULT_FORMAT) ⇒ Class
Return the type given the mumur3 hash.
-
#get_by_type(type, format = DEFAULT_FORMAT) ⇒ String
Return the mumur3 hash of the given the type.
-
#include?(key, opts = {}) ⇒ Boolean
Look the key up in the cache.
-
#initialize ⇒ AclTypeCache
constructor
A new instance of AclTypeCache.
Constructor Details
#initialize ⇒ AclTypeCache
Returns a new instance of AclTypeCache.
16 17 18 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 16 def initialize clear! end |
Instance Method Details
#add(type) ⇒ true|false
Add the type to the cashe. This will add the type for all know formats
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 23 def add(type) if SUPPORTED_FORMATS.all? { |format| @types[format].has_key?(type) } false else SUPPORTED_FORMATS.each do |format| h = to_murmur32(type, format) @types[format][type] = h @hashes[format][h] = type end @legacy_types_by_hash[type.to_s.split(/::/)[-1].snake_case] = type true end end |
#clear! ⇒ Object
Clear the internal hashes.
77 78 79 80 81 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 77 def clear! @types = SUPPORTED_FORMATS.each_with_object({}) { |v, acc| acc[v] = {} } @hashes = SUPPORTED_FORMATS.each_with_object({}) { |v, acc| acc[v] = {} } @legacy_types_by_hash = {} end |
#dump_hashes(format = DEFAULT_FORMAT) ⇒ Hash
Dump the hashes hash
101 102 103 104 105 106 107 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 101 def dump_hashes(format=DEFAULT_FORMAT) if @hashes.has_key?(format) @hashes[format] else raise ACL::UnknownTypeFormat, "Uknown format: #{format}" end end |
#dump_types(format = DEFAULT_FORMAT) ⇒ Hash
Dump the type hash
88 89 90 91 92 93 94 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 88 def dump_types(format=DEFAULT_FORMAT) if @types.has_key?(format) @types[format] else raise ACL::UnknownTypeFormat, "Uknown format: #{format}" end end |
#get_by_hash(type, format = DEFAULT_FORMAT) ⇒ Class
Return the type given the mumur3 hash
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 44 def get_by_hash(type, format=DEFAULT_FORMAT) if t = dump_hashes(format)[type] t else if t = @legacy_types_by_hash[type.to_s] t else raise ACL::UnknownError, "Unknown ACL: #{t}" end end end |
#get_by_type(type, format = DEFAULT_FORMAT) ⇒ String
Return the mumur3 hash of the given the type
61 62 63 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 61 def get_by_type(type, format=DEFAULT_FORMAT) dump_types(format)[type].tap { |t| raise ACL::UnknownError, "Unknown ACL: #{t}" if type.nil? } end |
#include?(key, opts = {}) ⇒ Boolean
Look the key up in the cache. This defaults to the key being the hash. If :by_type => true is passed in as the second argument then it will perform the lookup in the type hash.
68 69 70 71 72 73 74 |
# File 'lib/smith/messaging/acl_type_cache.rb', line 68 def include?(key, opts={}) if opts[:by_type] !get_by_type(key, opts.fetch(:format, DEFAULT_FORMAT)).nil? else !get_by_hash(key, opts.fetch(:format, DEFAULT_FORMAT)).nil? end end |