Class: Smith::AclTypeCache

Inherits:
Object
  • Object
show all
Includes:
MurmurHash3, Singleton
Defined in:
lib/smith/messaging/acl_type_cache.rb

Instance Method Summary collapse

Constructor Details

#initializeAclTypeCache

Returns a new instance of AclTypeCache.



13
14
15
# File 'lib/smith/messaging/acl_type_cache.rb', line 13

def initialize
  clear!
end

Instance Method Details

#add(type) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/smith/messaging/acl_type_cache.rb', line 17

def add(type)
  if @types[type]
    false
  else
    h = to_murmur32(type)
    @types[type] = h
    @hashes[h] = type
    @legacy_types_by_hash[type.to_s.split(/::/)[-1].snake_case] = type
    true
  end
end

#clear!Object

Clear the internal hashes.



54
55
56
57
58
# File 'lib/smith/messaging/acl_type_cache.rb', line 54

def clear!
  @types = {}
  @hashes = {}
  @legacy_types_by_hash = {}
end

#dump_hashesObject

Dump the hashes hash



66
67
68
# File 'lib/smith/messaging/acl_type_cache.rb', line 66

def dump_hashes
  @hashes
end

#dump_typesObject

Dump the type hash



61
62
63
# File 'lib/smith/messaging/acl_type_cache.rb', line 61

def dump_types
  @types
end

#get_by_hash(type) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/smith/messaging/acl_type_cache.rb', line 29

def get_by_hash(type)
  if @hashes[type]
    @hashes[type]
  elsif @legacy_types_by_hash[type.to_s]
    @legacy_types_by_hash[type.to_s]
  end
end

#get_by_type(type) ⇒ Object



37
38
39
# File 'lib/smith/messaging/acl_type_cache.rb', line 37

def get_by_type(type)
  @types[type]
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.

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/smith/messaging/acl_type_cache.rb', line 45

def include?(key, opts={})
  if opts[:by_type]
    !get_by_type(key).nil?
  else
    !get_by_hash(key).nil?
  end
end