From bc6e1ddc3dfc194620990716a06bf926b2d352ce Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Thu, 29 Aug 2013 16:37:56 +0200 Subject: [PATCH] Make implementation of Stdout more readable --- app/models/stdout.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/models/stdout.rb b/app/models/stdout.rb index e4fc526..af5954d 100644 --- a/app/models/stdout.rb +++ b/app/models/stdout.rb @@ -11,20 +11,26 @@ class Stdout def each File.open(data_path, 'rb') do |file| File.foreach(timing_path) do |line| - delay, size = TimingParser.parse_line(line) - yield(delay, file.read(size).force_encoding('utf-8')) + yield(*delay_and_data_for_line(file, line)) end end end def each_until(seconds) - time = 0 - each do |delay, frame_data| - time += delay - break if time > seconds + seconds -= delay + break if seconds <= 0 yield(delay, frame_data) end end + private + + def delay_and_data_for_line(file, line) + delay, size = TimingParser.parse_line(line) + data = file.read(size).force_encoding('utf-8') + + [delay, data] + end + end