Class: WCC::Contentful::Store::MemoryStore

Inherits:
Base
  • Object
show all
Defined in:
lib/wcc/contentful/store/memory_store.rb

Defined Under Namespace

Classes: Query

Instance Method Summary collapse

Methods inherited from Base

#find_by, #index

Constructor Details

#initializeMemoryStore

Returns a new instance of MemoryStore.



5
6
7
8
# File 'lib/wcc/contentful/store/memory_store.rb', line 5

def initialize
  super
  @hash = {}
end

Instance Method Details

#delete(key) ⇒ Object



19
20
21
22
23
# File 'lib/wcc/contentful/store/memory_store.rb', line 19

def delete(key)
  mutex.with_write_lock do
    @hash.delete(key)
  end
end

#find(key) ⇒ Object



29
30
31
32
33
# File 'lib/wcc/contentful/store/memory_store.rb', line 29

def find(key)
  mutex.with_read_lock do
    @hash[key]
  end
end

#find_all(content_type:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/wcc/contentful/store/memory_store.rb', line 35

def find_all(content_type:)
  relation = mutex.with_read_lock { @hash.values }

  relation =
    relation.reject do |v|
      value_content_type = v.dig('sys', 'contentType', 'sys', 'id')
      value_content_type.nil? || value_content_type != content_type
    end
  Query.new(relation)
end

#keysObject



25
26
27
# File 'lib/wcc/contentful/store/memory_store.rb', line 25

def keys
  mutex.with_read_lock { @hash.keys }
end

#set(key, value) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/wcc/contentful/store/memory_store.rb', line 10

def set(key, value)
  value = value.deep_dup.freeze
  mutex.with_write_lock do
    old = @hash[key]
    @hash[key] = value
    old
  end
end