Class: DicomS::SharedSettings
- Inherits:
-
Object
- Object
- DicomS::SharedSettings
- Defined in:
- lib/dicoms/shared_settings.rb
Overview
Shared Settings are Settings stored in a SharedFile so they can be used concurrently from different processes.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ SharedSettings
constructor
A new instance of SharedSettings.
-
#read ⇒ Object
Read a shared file and obtain a Settings object.
-
#update(&blk) ⇒ Object
To make sure contents are not changed between reading and writing use update:.
-
#write(data) ⇒ Object
Use this only if the contents written is independet of the previous content (i.e. no need to read, the change the data and write it back).
Constructor Details
#initialize(name, options = {}) ⇒ SharedSettings
Returns a new instance of SharedSettings.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dicoms/shared_settings.rb', line 9 def initialize(name, = {}) @file = SharedFile.new(name) @format = [:format] @compact = [:compact] unless @format if File.extname(@file.name) == '.json' @format = :json else @format = :yaml end end contents = [:initial_contents] if contents && !@file.exists? # Create with given initial contents write contents end contents = [:replace_contents] write contents if contents end |
Instance Method Details
#read ⇒ Object
Read a shared file and obtain a Settings object
counter = shared_settings.read.counter
33 34 35 |
# File 'lib/dicoms/shared_settings.rb', line 33 def read decode @file.read end |
#update(&blk) ⇒ Object
To make sure contents are not changed between reading and writing use update:
shared_settings.update do |data|
# modify data and return modified data
data.counter += 1
data
end
46 47 48 49 50 |
# File 'lib/dicoms/shared_settings.rb', line 46 def update(&blk) @file.update do |data| encode blk.call(decode(data)) end end |
#write(data) ⇒ Object
Use this only if the contents written is independet of the previous content (i.e. no need to read, the change the data and write it back)
shared_settings.write Setting[counter: 0
58 59 60 |
# File 'lib/dicoms/shared_settings.rb', line 58 def write(data) @file.write encode(data) end |