Class: Del::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/del/repository.rb

Overview

This class is a facade for backend data storage.

Instance Method Summary collapse

Constructor Details

#initialize(storage: {}, mapper:) ⇒ Repository

Returns a new instance of Repository.



6
7
8
9
10
# File 'lib/del/repository.rb', line 6

def initialize(storage: {}, mapper:)
  @storage = storage
  @mapper = mapper
  @lock = Mutex.new
end

Instance Method Details

#[](id) ⇒ Object



12
13
14
# File 'lib/del/repository.rb', line 12

def [](id)
  find(id)
end

#allObject



22
23
24
25
26
27
28
# File 'lib/del/repository.rb', line 22

def all
  @lock.synchronize do
    @storage.map do |(_, value)|
      @mapper.map_from(value)
    end
  end
end

#find(id) ⇒ Object



16
17
18
19
20
# File 'lib/del/repository.rb', line 16

def find(id)
  @lock.synchronize do
    @mapper.map_from(@storage[id.to_s])
  end
end

#upsert(id, attributes = {}) ⇒ Object



30
31
32
33
34
# File 'lib/del/repository.rb', line 30

def upsert(id, attributes = {})
  @lock.synchronize do
    @storage[id.to_s] = attributes
  end
end