Class: MessageDir
- Inherits:
-
Object
- Object
- MessageDir
- Defined in:
- lib/message_dir.rb,
lib/message_dir/message.rb,
lib/message_dir/version.rb
Defined Under Namespace
Constant Summary collapse
- SPOTS =
%w(tmp new cur)
- VERSION =
"1.0.2"
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #create_tree ⇒ Object
- #cur(msg) ⇒ Object
-
#initialize(path) ⇒ MessageDir
constructor
A new instance of MessageDir.
- #message(uuid) ⇒ Object
- #messages(where = nil) ⇒ Object (also: #msgs)
- #new(&block) ⇒ Object
- #process(msg, mode = File::RDONLY, &block) ⇒ Object
- #rm(msg) ⇒ Object
Constructor Details
#initialize(path) ⇒ MessageDir
Returns a new instance of MessageDir.
11 12 13 14 15 |
# File 'lib/message_dir.rb', line 11 def initialize(path) @path = MessageDir::Path.new(path) create_tree end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/message_dir.rb', line 9 def path @path end |
Instance Method Details
#create_tree ⇒ Object
17 18 19 20 21 |
# File 'lib/message_dir.rb', line 17 def create_tree SPOTS.each do |spot| FileUtils.mkdir_p @path.send(spot) end end |
#cur(msg) ⇒ Object
50 51 52 53 |
# File 'lib/message_dir.rb', line 50 def cur(msg) return if msg.spot == :cur msg.move(path.cur) end |
#message(uuid) ⇒ Object
72 73 74 75 |
# File 'lib/message_dir.rb', line 72 def (uuid) uuid = uuid.to_guid if uuid.is_a? SimpleUUID::UUID .select { |msg| msg.guid == uuid }.first end |
#messages(where = nil) ⇒ Object Also known as: msgs
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/message_dir.rb', line 55 def (where=nil) spots = where.nil? ? SPOTS : [ where ] # find all the wanted paths paths = spots.collect do |spot| Dir[File.join(path.send(spot), '*')].select do |path| path !~ /\.(lock|err)$/ && File.file?(path) end end.flatten # load all the wanted messages paths.collect do |path| Message.load(self, path) end end |
#new(&block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/message_dir.rb', line 23 def new(&block) uuid = SimpleUUID::UUID.new msg = Message.new(self, path.new, uuid) if block_given? begin msg.move(path.tmp) msg.open(File::TRUNC|File::WRONLY) do |fh| yield fh end msg.move(path.new) rescue Exception => ex # silently ignore errors ensure # make sure the message is written msg.close end end msg.open(File::RDONLY) msg.seek(0,0) return msg end |
#process(msg, mode = File::RDONLY, &block) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/message_dir.rb', line 77 def process(msg, mode=File::RDONLY, &block) raise "No block given" if !block_given? # move to tmp and open with block for the given mode before = msg.spot msg.move(path.tmp) unless before == :tmp msg.lock do msg.open(mode, &block) end # move back re-open with default mode msg.move(path.send(before)) unless before == :tmp msg.open end |
#rm(msg) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/message_dir.rb', line 92 def rm(msg) return false if msg.locked? files = [ msg.path ] files << msg.error_file if msg.error? File.unlink(*files) == files.count ? true : false end |