lib/ionian/extension/io.rb in ionian-0.7.0 vs lib/ionian/extension/io.rb in ionian-0.8.0
- old
+ new
@@ -37,21 +37,22 @@
#
def has_data? timeout: 0
::IO.select([self], nil, nil, timeout) ? true : false
end
- # @return [Regexp] Regular expression used for {#read_match}.
+ # @return [Regexp] the regular expression used for {#read_match}.
def expression
@ionian_expression
end
# Set the expression to match against the read buffer.
# Can be a regular expression specifying capture groups,
# or a string specifying the separator or line terminator
# sequence. It is possible to use named captures in a
# regex, which allows for convienient accessors like
# match[:parameter].
+ # @param exp [Regexp, String] Match expression.
def expression= exp
@ionian_expression = exp
@ionian_expression = Regexp.new "(.*?)#{expression}" if exp.is_a? String
end
@@ -82,12 +83,12 @@
#
#
# @yieldparam match [MatchData] If there are multiple matches, the block
# is called multiple times.
#
- # @return [Array<MatchData>, nil] matches.
- # Nil if no data was received within the timeout period.
+ # @return [Array<MatchData>] matches.
+ # Empty array if no data was received within the timeout period.
#
#
# @option kwargs [Numeric] :timeout (nil) Timeout in seconds IO::select
# will block. Blocks indefinitely by default. Set to 0 for nonblocking.
#
@@ -112,11 +113,11 @@
exp = kwargs.fetch :expression, @ionian_expression
exp = Regexp.new "(.*?)#{exp}" if exp.is_a? String
unless skip_select
- return nil unless self.has_data? timeout: timeout
+ return [] unless self.has_data? timeout: timeout
end
@matches = []
# TODO: Implement an option for number of bytes or timeout to throw away
@@ -163,29 +164,20 @@
def run_match **kwargs
@run_match_thread ||= Thread.new do
Thread.current.thread_variable_set :match_thread_running, true
begin
while not closed? do
- matches = read_match **kwargs
- matches.each { |match| notify_match_handlers match } if matches
+ read_match(**kwargs).each { |match| notify_match_handlers match }
end
rescue Exception => e
notify_error_handlers e
ensure
@run_match_thread = nil
end
end
end
- def run_match_is_running?
- return true if \
- @run_match_thread and
- @run_match_thread.thread_variable_get(:match_thread_running)
-
- false
- end
-
# Erase the data in the IO and Ionian buffers.
# This is typically handled automatically.
def purge
# Erase IO buffer.
read_all
@@ -209,11 +201,11 @@
alias_method :on_match, :register_match_handler
# @deprecated Use {#register_match_handler} instead.
def register_observer &block
- STDOUT.puts "WARN: Call to deprecated method #{__method__}"
+ STDOUT.puts "WARNING: Call to deprecated method: #{__method__}"
register_match_handler &block
end
# Unregister a block from being called when matched data is received.
def unregister_match_handler &block
@@ -221,10 +213,10 @@
block
end
# @deprecated Use {#unregister_match_handler} instead.
def unregister_observer &block
- STDOUT.puts "WARN: Call to deprecated method #{__method__}"
+ STDOUT.puts "WARNING: Call to deprecated method: #{__method__}"
unregister_match_handler &block
end
# Register a block to be called when {#run_match} raises an error.
# Method callbacks can be registered with &object.method(:method).
\ No newline at end of file