Class: Rmk::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/rmk/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, iniobj) ⇒ Storage

Returns a new instance of Storage.

Parameters:

  • iniobj (Object)

    init inside obj



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

#saveObject

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

Yield Parameters:

  • data (Hash)

    inside Hash obj

Returns:

  • (Object)

    block’s result



23
# File 'lib/rmk/storage.rb', line 23

def sync(&cmd) @mutex.synchronize{cmd.call @data} end

#wait_readyObject

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