Class: EventMachine::FS::Command

Inherits:
EM::SystemCommand
  • Object
show all
Defined in:
lib/em-fs/fs/command.rb

Constant Summary collapse

PROGRESS_REGEXP =
/([^\n]+)\n[ ]+([^ ]+)[ ]+(.+)%/.freeze

Instance Method Summary collapse

Instance Method Details

#execute(&block) ⇒ Object

Invokes ‘#execute` of super-class and adds a progress matcher.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/em-fs/fs/command.rb', line 9

def execute &block
  super &block

  last_progress    = 0
  last_progress_at = Time.new.to_f
  stdout.match PROGRESS_REGEXP, match: :last, in: :output do |file, total_bytes, percentage|
    progress = total_bytes.gsub(/[^\d]/,'').to_i
    progress_at = Time.new.to_f
    speed = (progress - last_progress) / (progress_at - last_progress_at)

    receive_progress(file, progress, percentage.to_i, speed)

    last_progress    = progress
    last_progress_at = progress_at
  end

  self
end

#progress {|file, bytes| ... } ⇒ Object

Defines a progress callback.

Yields:

  • The block to be stored as callback for progress events.

Yield Parameters:

  • file (String)

    The file that´s been updated.

  • bytes (Integer)

    The bytes moved or copied.



48
49
50
# File 'lib/em-fs/fs/command.rb', line 48

def progress &block
  progress_callbacks << block
end

#receive_progress(*args) ⇒ Object

Is called when ever a ‘EM::FilesystemCommand` stdout updates the line is matched the `EM::FilesystemCommand::PROGRESS_REGEXP`.

Calls all defined callbacks for progress events.

Parameters:

  • file (String)

    The file that´s been updated.

  • bytes (Integer)

    The bytes moved or copied.



36
37
38
39
40
# File 'lib/em-fs/fs/command.rb', line 36

def receive_progress *args
  progress_callbacks.each do |cb|
    cb.call(*args)
  end
end