Class: Rmk::Storage
- Inherits:
-
Object
- Object
- Rmk::Storage
- Defined in:
- lib/rmk/storage.rb
Instance Method Summary collapse
-
#data! ⇒ Object
get inside Hash obj without sync protected.
-
#initialize(file, iniobj) ⇒ Storage
constructor
A new instance of Storage.
-
#method_missing(name, *parms, &cmd) ⇒ Object
redirect undef method to inside data obj with sync protected, often used for single operation.
-
#save ⇒ Object
save data to disk file.
-
#sync {|data| ... } ⇒ Object
run block in mutex sync protected.
-
#wait_ready ⇒ Object
wait for storage ready to read and write.
Constructor Details
#initialize(file, iniobj) ⇒ Storage
Returns a new instance of Storage.
7 8 9 10 11 |
# File 'lib/rmk/storage.rb', line 7 def initialize(file, iniobj) @mutex = Thread::Mutex.new @file = file @data = File.exist?(@file) ? Rmk::Schedule.new_thread!{Marshal.load IO.binread @file} : iniobj end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *parms, &cmd) ⇒ Object
redirect undef method to inside data obj with sync protected, often used for single operation
29 |
# File 'lib/rmk/storage.rb', line 29 def method_missing(name, *parms, &cmd) @mutex.synchronize{@data.send name, *parms, &cmd} end |
Instance Method Details
#data! ⇒ Object
get inside Hash obj without sync protected
26 |
# File 'lib/rmk/storage.rb', line 26 def data!; @data end |
#save ⇒ Object
save data to disk file
18 |
# File 'lib/rmk/storage.rb', line 18 def save; @mutex.synchronize{IO.binwrite @file, Marshal.dump(@data)} end |
#sync {|data| ... } ⇒ Object
run block in mutex sync protected
23 |
# File 'lib/rmk/storage.rb', line 23 def sync(&cmd) @mutex.synchronize{cmd.call @data} end |
#wait_ready ⇒ Object
Note:
before call this method storage is not ready, can’t call any follow methods
wait for storage ready to read and write
15 |
# File 'lib/rmk/storage.rb', line 15 def wait_ready; @mutex.synchronize{@data = @data.value if Thread === @data} end |