Class: DicomS::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/dicoms/progress.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, options = {}) ⇒ Progress

Returns a new instance of Progress.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dicoms/progress.rb', line 3

def initialize(description, options={})
  options = CommandOptions[options]
  filename = options.path_option(:progress)
  @progress = options[:initial_progress] || 0
  # TODO: if filename == :console, show progress on the console
  if filename
    @file = SharedSettings.new(
      filename,
      replace_contents: {
        process: description,
        subprocess: options[:subprocess],
        progress: @progress
      }
    )
  else
    @file = nil
  end
  @subprocess_start = @subprocess_size = @subprocess_percent = nil
end

Instance Attribute Details

#progressObject (readonly)

Returns the value of attribute progress.



23
24
25
# File 'lib/dicoms/progress.rb', line 23

def progress
  @progress
end

Instance Method Details

#begin_subprocess(description, percent = nil, size = 0) ⇒ Object

Begin a subprocess which represent ‘percent` of the total process. The subprocess will be measured with values up to `size`



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dicoms/progress.rb', line 47

def begin_subprocess(description, percent=nil, size=0)
  end_subprocess if @subprocess_start
  @subprocess_start = @progress
  percent ||= 100
  if percent < 0
    # interpreted as percent of what's lef
    percent = (100 - @progress)*(-percent)/100.0
  end
  percent = [percent, 100 - @progress].min
  # @subprocess_end = @progress + percent
  @subprocess_size = size.to_f
  @subprocess_percent = percent
  update @progress, description
end

#end_subprocessObject



73
74
75
76
77
78
# File 'lib/dicoms/progress.rb', line 73

def end_subprocess
  raise "Subprocess not started" unless @subprocess_start
  @progress = @subprocess_start + @subprocess_percent
  @subprocess_start = @subprocess_size = @subprocess_percent = nil
  update @progress
end

#finishObject



40
41
42
# File 'lib/dicoms/progress.rb', line 40

def finish
  update 100, 'finished'
end

#finished?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dicoms/progress.rb', line 25

def finished?
  @progress >= 100
end

#update(value, subprocess = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/dicoms/progress.rb', line 29

def update(value, subprocess = nil)
  @progress = value
  if @file
    @file.update do |data|
      data.progress = @progress
      data.subprocess = subprocess if subprocess
      data
    end
  end
end

#update_subprocess(value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/dicoms/progress.rb', line 62

def update_subprocess(value)
  raise "Subprocess not started" unless @subprocess_start
  sub_fraction = value/@subprocess_size
  @progress = @subprocess_start + @subprocess_percent*sub_fraction
  if @subprocess_size < 20 || (value % 10) == 0
    # frequently updated processes don't update the file every
    # fime to avoid the overhead (just 1 in 10 times)
    update @progress
  end
end