lib/em-systemcommand/pipe.rb in em-systemcommand-1.2.0 vs lib/em-systemcommand/pipe.rb in em-systemcommand-2.0.0
- old
+ new
@@ -15,16 +15,24 @@
def output
@outputbuffer.string
end
- # Convenience method to create a callback that matches a regular expression
+ ##
+ # Convenience method to create a callback that matches a regular
+ # expression
def match regexp, opt = {}, &block
- opt = { in: :line }.merge opt
+ opt = { in: :line, match: :first }.merge opt
(opt[:in] == :output ? receive_update_callbacks : receive_line_callbacks) << lambda do |data|
- if m = data.match(regexp)
- block.call m.to_a
+ matches = data.scan regexp
+ if matches.length > 0
+ case opt[:match]
+ when :first
+ block.call *matches[0]
+ when :last
+ block.call *matches[matches.length-1]
+ end
end
end
end
# Invoked when line was received
@@ -120,9 +128,10 @@
rescue Exception => e
# ignore errors, when the io object might be closed already
end
end
+ ##
# Invoked when the connection is terminated. Calls
# `unbind(@name)` on master.
def unbind
self.close
@master.unbind(@name)