Class: Hold::Serialized::IdentitySetRepository

Inherits:
Object
  • Object
show all
Includes:
IdentitySetRepository
Defined in:
lib/hold/serialized/identity_set_repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IdentitySetRepository

#cell, #get_many_by_ids, #id_cell, #load, #reload, #update, #update_by_id

Methods included from Hold::SetRepository

#can_get_class?, #can_set_class?, #get_all, #store_new

Constructor Details

#initialize(cache, serializer, key_prefix = nil) ⇒ IdentitySetRepository

Returns a new instance of IdentitySetRepository.



8
9
10
11
12
# File 'lib/hold/serialized/identity_set_repository.rb', line 8

def initialize(cache, serializer, key_prefix=nil)
  @cache = cache
  @serializer = serializer
  @key_prefix = key_prefix
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



6
7
8
# File 'lib/hold/serialized/identity_set_repository.rb', line 6

def cache
  @cache
end

#key_prefixObject (readonly)

Returns the value of attribute key_prefix.



6
7
8
# File 'lib/hold/serialized/identity_set_repository.rb', line 6

def key_prefix
  @key_prefix
end

#serializerObject (readonly)

Returns the value of attribute serializer.



6
7
8
# File 'lib/hold/serialized/identity_set_repository.rb', line 6

def serializer
  @serializer
end

Instance Method Details

#allocates_ids?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/hold/serialized/identity_set_repository.rb', line 18

def allocates_ids?
  false
end

#cache_key(key) ⇒ Object



14
15
16
# File 'lib/hold/serialized/identity_set_repository.rb', line 14

def cache_key(key)
  @key_prefix ? @key_prefix + key.to_s : key.to_s
end

#contains?(object) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/hold/serialized/identity_set_repository.rb', line 33

def contains?(object)
  id = object.id or raise MissingIdentity
  contains_id?(id)
end

#contains_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/hold/serialized/identity_set_repository.rb', line 53

def contains_id?(id)
  @cache.has_key?(cache_key(id))
end

#delete(object) ⇒ Object



28
29
30
31
# File 'lib/hold/serialized/identity_set_repository.rb', line 28

def delete(object)
  id = object.id or raise MissingIdentity
  delete_id(id)
end

#delete_id(id) ⇒ Object



49
50
51
# File 'lib/hold/serialized/identity_set_repository.rb', line 49

def delete_id(id)
  @cache.clear_key(cache_key(id))
end

#get_by_id(id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/hold/serialized/identity_set_repository.rb', line 38

def get_by_id(id)
  json = @cache.get_with_key(cache_key(id))
  if json
    string_hash = @serializer.deserialize(json)
    string_hash.inject({}) do |memo, (k,v)|
      memo[k.to_sym] = v
      memo
    end
  end
end

#store(object) ⇒ Object



22
23
24
25
26
# File 'lib/hold/serialized/identity_set_repository.rb', line 22

def store(object)
  id = object.id or raise MissingIdentity
  @cache.set_with_key(cache_key(id), @serializer.serialize(object))
  object
end