Class: MessageDir::Message
- Inherits:
-
Object
- Object
- MessageDir::Message
show all
- Defined in:
- lib/message_dir/message.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(master, path, uuid) ⇒ Message
Returns a new instance of Message.
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/message_dir/message.rb', line 3
def initialize(master, path, uuid)
@master = master
@path = path
@uuid = uuid
@io = nil
self.create if !File.exists?(self.path)
@locked = File.exists?(self.lock_file)
open
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
155
156
157
158
159
|
# File 'lib/message_dir/message.rb', line 155
def method_missing(method, *args, &block)
super
rescue NameError, NoMethodError => e
@io.send(method, *args, &block)
end
|
Class Method Details
.load(master, path) ⇒ Object
14
15
16
|
# File 'lib/message_dir/message.rb', line 14
def self.load(master, path)
self.new(master, File.dirname(path), SimpleUUID::UUID.new(File.basename(path)))
end
|
Instance Method Details
#==(other) ⇒ Object
92
93
94
|
# File 'lib/message_dir/message.rb', line 92
def ==(other)
other.path == self.path && self.guid == other.guid
end
|
#close ⇒ Object
76
77
78
79
80
|
# File 'lib/message_dir/message.rb', line 76
def close
@io.close unless closed?
rescue
true
end
|
#closed? ⇒ Boolean
82
83
84
85
86
|
# File 'lib/message_dir/message.rb', line 82
def closed?
@io.closed?
rescue
return true
end
|
#create ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/message_dir/message.rb', line 30
def create
open(File::CREAT|File::WRONLY) do |f|
f.syswrite("i")
f.truncate(0)
end
end
|
#cur! ⇒ Object
88
89
90
|
# File 'lib/message_dir/message.rb', line 88
def cur!
@master.cur(self)
end
|
#error(&block) ⇒ Object
140
141
142
143
144
145
146
147
|
# File 'lib/message_dir/message.rb', line 140
def error(&block)
unless block_given?
return nil unless self.error?
File.read(self.error_file)
else
File.open(self.error_file, File::WRONLY|File::CREAT|File::TRUNC, &block)
end
end
|
#error=(msg) ⇒ Object
149
150
151
152
153
|
# File 'lib/message_dir/message.rb', line 149
def error=(msg)
self.error do |fh|
fh.puts msg
end
end
|
#error? ⇒ Boolean
136
137
138
|
# File 'lib/message_dir/message.rb', line 136
def error?
File.exists?(self.error_file)
end
|
#error_file ⇒ Object
132
133
134
|
# File 'lib/message_dir/message.rb', line 132
def error_file
self.path + ".err"
end
|
#fh ⇒ Object
55
56
57
|
# File 'lib/message_dir/message.rb', line 55
def fh
@io
end
|
#guid ⇒ Object
22
23
24
|
# File 'lib/message_dir/message.rb', line 22
def guid
@uuid.to_guid
end
|
#lock ⇒ Object
Also known as:
lock!
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/message_dir/message.rb', line 104
def lock
return if locked?
File.open(lock_file, File::CREAT|File::WRONLY) do |lf|
res = lf.flock(File::LOCK_EX|File::LOCK_NB)
raise "Could not obtain a lock on the lock file" if res == false
lf.flock(File::LOCK_UN)
end
if block_given?
yield
unlock
else
@locked = true
end
end
|
#lock_file ⇒ Object
100
101
102
|
# File 'lib/message_dir/message.rb', line 100
def lock_file
"#{path}.lock"
end
|
#locked? ⇒ Boolean
96
97
98
|
# File 'lib/message_dir/message.rb', line 96
def locked?
@locked == true ? true : false || File.exists?(lock_file)
end
|
#move(path) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/message_dir/message.rb', line 59
def move(path)
raise "Can't move a locked file" if locked?
close
if self.error?
FileUtils.mv(self.path + ".err", File.join(path, self.guid + ".err"))
end
FileUtils.mv(self.path, File.join(path, self.guid))
@path = path
open
end
|
#open(mode = File::RDONLY, &block) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/message_dir/message.rb', line 42
def open(mode=File::RDONLY, &block)
close
mode |= File::SYNC unless mode & File::SYNC == File::SYNC
@io = File.open(self.path, mode)
if block_given?
yield self
end
end
|
#path ⇒ Object
18
19
20
|
# File 'lib/message_dir/message.rb', line 18
def path
File.join(@path, @uuid.to_guid)
end
|
#process(mode = File::RDONLY, &block) ⇒ Object
38
39
40
|
# File 'lib/message_dir/message.rb', line 38
def process(mode=File::RDONLY, &block)
@master.process(self, mode, &block)
end
|
#rm ⇒ Object
72
73
74
|
# File 'lib/message_dir/message.rb', line 72
def rm
@master.rm(self)
end
|
#spot ⇒ Object
26
27
28
|
# File 'lib/message_dir/message.rb', line 26
def spot
File.basename(@path).to_sym
end
|
#unlock ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/message_dir/message.rb', line 122
def unlock
File.open(lock_file, File::CREAT|File::WRONLY) do |lf|
res = lf.flock(File::LOCK_EX|File::LOCK_NB)
raise "Could not obtain a lock on the lock file" if res == false
lf.flock(File::LOCK_UN)
File.unlink(lock_file)
end
@locked = false
end
|