Class: ChefApply::UI::PlainTextElement

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_apply/ui/plain_text_element.rb

Instance Method Summary collapse

Constructor Details

#initialize(format, opts) ⇒ PlainTextElement

Returns a new instance of PlainTextElement.



21
22
23
24
25
# File 'lib/chef_apply/ui/plain_text_element.rb', line 21

def initialize(format, opts)
  @orig_format = format
  @format = format
  @output = opts[:output]
end

Instance Method Details

#auto_spinObject



74
# File 'lib/chef_apply/ui/plain_text_element.rb', line 74

def auto_spin; end

#errorObject



64
65
66
67
# File 'lib/chef_apply/ui/plain_text_element.rb', line 64

def error
  @err = true
  @succ = false
end

#run(&block) ⇒ Object



27
28
29
# File 'lib/chef_apply/ui/plain_text_element.rb', line 27

def run(&block)
  yield
end

#successObject



69
70
71
72
# File 'lib/chef_apply/ui/plain_text_element.rb', line 69

def success
  @succ = true
  @err = false
end

#update(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef_apply/ui/plain_text_element.rb', line 31

def update(params)
  # Some of this is particular to our usage -
  # prefix does not cause a text update, but does
  # change the prefix for future messages.
  if params.key?(:prefix)
    @format = @orig_format.gsub(":prefix", params[:prefix])
    return
  end

  if @succ
    ind = "OK"
    @succ = false
    log_method = :info
  elsif @err
    ind = "ERR"
    @err = false
    log_method = :error
  else
    log_method = :debug
    ind = " - "
  end

  # Since this is a generic type, we can replace any component
  # name in this regex - but for now :spinner is the only component
  # we're standing in for.
  msg = @format.gsub(/:spinner/, ind)
  params.each_pair do |k, v|
    msg.gsub!(/:#{k}/, v)
  end
  ChefApply::Log.send(log_method, msg)
  @output.puts(msg)
end