Class: Foruiman::LogStore

Inherits:
Object
  • Object
show all
Defined in:
lib/foruiman/log_store.rb

Defined Under Namespace

Classes: Record

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, capacity: 10_000, &listener) ⇒ LogStore

Returns a new instance of LogStore.



9
10
11
12
13
14
# File 'lib/foruiman/log_store.rb', line 9

def initialize(names, capacity: 10_000, &listener)
  @all = Foruiman::Ring.new(capacity)
  @buffers = names.to_h { |name| [name, Foruiman::Ring.new(capacity)] }
  @sequence = 0
  @listener = listener
end

Instance Attribute Details

#all ⇒ Object (readonly)

Returns the value of attribute all.



7
8
9
# File 'lib/foruiman/log_store.rb', line 7

def all
  @all
end

Instance Method Details

#[](name) ⇒ Object



16
17
18
# File 'lib/foruiman/log_store.rb', line 16

def [](name)
  name == :all ? all : @buffers.fetch(name)
end

#write(name:, stream:, pid:, text:, complete:, previous: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/foruiman/log_store.rb', line 20

def write(name:, stream:, pid:, text:, complete:, previous: nil)
  @sequence += 1 unless previous
  record = Record.new(sequence: previous ? previous.sequence : @sequence, name: name,
                      stream: stream, pid: pid, time: previous ? previous.time : Time.now.freeze,
                      text: text.dup.freeze, complete: complete)
  method = previous ? :replace : :append
  @buffers.fetch(name).public_send(method, record)
  all.public_send(method, record)
  @listener&.call(record)
  record
end