Class: Kernai::MediaStore

Inherits:
Object
  • Object
show all
Defined in:
lib/kernai/media_store.rb

Overview

Per-execution bag of Media objects keyed by id. Lives on the Context so parent and child agents (spawned through workflows) share the same store — a child can look up a media the parent produced, and a skill can resolve a media by id without having to thread the object through every intermediate message.

Instance Method Summary collapse

Constructor Details

#initializeMediaStore

Returns a new instance of MediaStore.



10
11
12
13
# File 'lib/kernai/media_store.rb', line 10

def initialize
  @store = {}
  @mutex = Mutex.new
end

Instance Method Details

#each(&block) ⇒ Object



26
27
28
# File 'lib/kernai/media_store.rb', line 26

def each(&block)
  @mutex.synchronize { @store.values.dup }.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/kernai/media_store.rb', line 34

def empty?
  size.zero?
end

#fetch(id) ⇒ Object



22
23
24
# File 'lib/kernai/media_store.rb', line 22

def fetch(id)
  @mutex.synchronize { @store[id] }
end

#put(media) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/kernai/media_store.rb', line 15

def put(media)
  raise ArgumentError, 'expected a Kernai::Media' unless media.is_a?(Media)

  @mutex.synchronize { @store[media.id] = media }
  media
end

#sizeObject



30
31
32
# File 'lib/kernai/media_store.rb', line 30

def size
  @mutex.synchronize { @store.size }
end