Class: RServiceBus2::SendAtStorageFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rservicebus2/sendat_storage/file.rb

Overview

Send at storage file

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ SendAtStorageFile

Returns a new instance of SendAtStorageFile.



6
7
8
9
10
# File 'lib/rservicebus2/sendat_storage/file.rb', line 6

def initialize(uri)
  RServiceBus2.log "SendAtStorageFile configured: #{uri.path}"
  @list = load(uri.path)
  @path = uri.path
end

Instance Method Details

#add(msg) ⇒ Object



22
23
24
25
# File 'lib/rservicebus2/sendat_storage/file.rb', line 22

def add(msg)
  @list << msg
  save
end

#allObject



27
28
29
# File 'lib/rservicebus2/sendat_storage/file.rb', line 27

def all
  @list
end

#delete(idx) ⇒ Object



31
32
33
34
# File 'lib/rservicebus2/sendat_storage/file.rb', line 31

def delete(idx)
  @list.delete_at(idx)
  save
end

#load(path) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rservicebus2/sendat_storage/file.rb', line 12

def load(path)
  return [] unless File.exist?(path)

  content = IO.read(path)

  return [] if content == ''

  RServiceBus2.safe_load(content)
end

#saveObject



36
37
38
39
# File 'lib/rservicebus2/sendat_storage/file.rb', line 36

def save
  content = YAML.dump(@list)
  File.open(@path, 'w') { |f| f.write(content) }
end