Class: Buffet::StatusMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/buffet/status_message.rb

Instance Method Summary (collapse)

Constructor Details

- (StatusMessage) initialize(should_display = false)

A new instance of StatusMessage



9
10
11
12
13
14
15
# File 'lib/buffet/status_message.rb', line 9

def initialize(should_display=false)
  @message = ""
  @show_progress = false
  @progress = 0
  @max_progress = 0
  @should_display = should_display
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_sym, *args, &block)

If someone has set() the status to a hash, you can get the values out by doing status.keyword_in_hash



73
74
75
# File 'lib/buffet/status_message.rb', line 73

def method_missing(method_sym, *args, &block)
  @message[method_sym]
end

Instance Method Details

- (Object) increase_progress(progress_regex, expected, command)

Run the command COMMAND, and every time an output line matches PROGRESS_REGEX, add to self



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/buffet/status_message.rb', line 39

def increase_progress(progress_regex, expected, command)
  start_progress(expected)
  # It's necessary to split along && because passing in multiple commands to
  # popen3 does not appear to work.
  command.split('&&').map do |command|
    Wopen3.popen3(*command.split(" ")) do |stdin, stdout, stderr|
      threads = []
      threads << Thread.new(stdout) do |out|
        out.each do |line|
          if progress_regex =~ line
            add_to_progress
          end
        end
      end

      threads << Thread.new(stderr) do |out|
        out.each do |line|
          puts line
        end
      end

      threads.each do |thread|
        thread.join
      end
    end
  end

  if $?.exitstatus != 0
    set "Command #{command} failed."
  end
end

- (Object) set(message)

Set the status message. Support for either setting it to a string or a hash. If it's a string, StatusMessage mimics a string; similarly with hashes.



20
21
22
23
24
25
# File 'lib/buffet/status_message.rb', line 20

def set(message)
  @message = message
  @show_progress = false

  display
end

- (Object) to_s

Convenience. This makes things like puts status work without any more calls.



29
30
31
32
33
34
35
# File 'lib/buffet/status_message.rb', line 29

def to_s
  if @show_progress
    "#{@message} (#{@progress} of #{@max_progress})"
  else
    "#{@message}"
  end
end