class Smith::AclTypeCache
Constants
- DEFAULT_FORMAT
- SUPPORTED_FORMATS
Public Class Methods
# File lib/smith/messaging/acl_type_cache.rb, line 15 def initialize clear! end
Public Instance Methods
Add the type to the cashe. This will add the type for all know formats @param type [Class] the type to add @return [true|false] true if the type was added or false if it already exists.
# File lib/smith/messaging/acl_type_cache.rb, line 22 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 the internal hashes.
# File lib/smith/messaging/acl_type_cache.rb, line 76 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 the hashes hash @param format [Symbol] the format of the mumor3 hash. Defaults to
Smith::AclTypeCache::DEFAULT_FORMAT
@return [Hash] @raise [Smith::ACL::UnknownTypeFormat] raised when an unknown format is given
# File lib/smith/messaging/acl_type_cache.rb, line 100 def dump_hashes(format=DEFAULT_FORMAT) if @hashes.has_key?(format) @hashes[format] else raise ACL::UnknownTypeFormat, "Uknown format: #{format}" end end
Dump the type hash @param format [Symbol] the format of the mumor3 hash. Defaults to
Smith::AclTypeCache::DEFAULT_FORMAT
@return [Hash] @raise [Smith::ACL::UnknownTypeFormat] raised when an unknown format is given
# File lib/smith/messaging/acl_type_cache.rb, line 87 def dump_types(format=DEFAULT_FORMAT) if @types.has_key?(format) @types[format] else raise ACL::UnknownTypeFormat, "Uknown format: #{format}" end end
Return the type given the mumur3 hash @param type [Sting] the mumur3 hash to lookup @param format [Symbol] the format of the mumor3 hash. Defaults to
Smith::AclTypeCache::DEFAULT_FORMAT
@return [Class] @raise [Smith::ACL::UnknownError] raised when an unknown ACL
is given
# File lib/smith/messaging/acl_type_cache.rb, line 43 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
Return the mumur3 hash of the given the type @param type [Class] the class to lookup @param format [Symbol] the format of the mumor3 hash. Defaults to
Smith::AclTypeCache::DEFAULT_FORMAT
@return [String]
# File lib/smith/messaging/acl_type_cache.rb, line 60 def get_by_type(type, format=DEFAULT_FORMAT) dump_types(format)[type].tap { |t| raise ACL::UnknownError, "Unknown ACL: #{t}" if type.nil? } end
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.
# File lib/smith/messaging/acl_type_cache.rb, line 67 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
Private Instance Methods
Convert the type to a murmor3 hash @param type [Class] the class to lookup @param format [Symbol] the format of the mumor3 hash. Defaults to
Smith::AclTypeCache::DEFAULT_FORMAT
@return [String] @raise [Smith::ACL::UnknownTypeFormat] raised when an unknown format is given
# File lib/smith/messaging/acl_type_cache.rb, line 116 def to_murmur32(type, format) case format when :string MurmurHash3::V32.murmur3_32_str_hash(type.to_s).to_s(36) when :binary MurmurHash3::V32.murmur3_32_str_hash(type.to_s) else raise ACL::UnknownTypeFormat, "Uknown format: #{format}" end end