Class: Higgs::FileLock
- Inherits:
-
Object
- Object
- Higgs::FileLock
- Defined in:
- lib/higgs/flock.rb
Constant Summary collapse
- CVS_ID =
for ident(1)
'$Id: flock.rb 841 2008-12-24 09:23:20Z toki $'
Instance Attribute Summary collapse
-
#read_only ⇒ Object
readonly
Returns the value of attribute read_only.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(path, read_only = false) ⇒ FileLock
constructor
A new instance of FileLock.
- #read_lock ⇒ Object
- #synchronize(mode = :EX) ⇒ Object
- #unlock ⇒ Object
- #write_lock ⇒ Object
Constructor Details
#initialize(path, read_only = false) ⇒ FileLock
Returns a new instance of FileLock.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/higgs/flock.rb', line 16 def initialize(path, read_only=false) @path = path @read_only = read_only rdwr_mode = (@read_only) ? File::RDONLY : File::RDWR begin @f = File.open(path, rdwr_mode | File::CREAT | File::EXCL, 0660) rescue Errno::EEXIST @f = File.open(path, rdwr_mode, 0660) end end |
Instance Attribute Details
#read_only ⇒ Object (readonly)
Returns the value of attribute read_only.
27 28 29 |
# File 'lib/higgs/flock.rb', line 27 def read_only @read_only end |
Class Method Details
.open(*args) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/higgs/flock.rb', line 72 def self.open(*args) flock = new(*args) begin r = yield(flock) ensure flock.close end r end |
Instance Method Details
#close ⇒ Object
47 48 49 50 51 |
# File 'lib/higgs/flock.rb', line 47 def close unlock @f.close nil end |
#read_lock ⇒ Object
29 30 31 32 |
# File 'lib/higgs/flock.rb', line 29 def read_lock @f.flock(File::LOCK_SH) nil end |
#synchronize(mode = :EX) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/higgs/flock.rb', line 53 def synchronize(mode=:EX) case (mode) when :SH read_lock when :EX write_lock else raise ArgumentError, "unknown lock mode: #{mode}" end begin r = yield ensure unlock end r end |
#unlock ⇒ Object
42 43 44 45 |
# File 'lib/higgs/flock.rb', line 42 def unlock @f.flock(File::LOCK_UN) nil end |
#write_lock ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/higgs/flock.rb', line 34 def write_lock if (@read_only) then raise 'read only' end @f.flock(File::LOCK_EX) nil end |