Class: Chef::Knife::ChopUI

Inherits:
UI
  • Object
show all
Includes:
ChopErrors
Defined in:
lib/chef/knife/chop/ui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, config) ⇒ ChopUI

Returns a new instance of ChopUI.



29
30
31
32
33
# File 'lib/chef/knife/chop/ui.rb', line 29

def initialize(logger, config)
  super($stdout, $stderr, $stdin, config)
  @logger = logger
  #define_ui_methods()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Raises:



82
83
84
85
86
# File 'lib/chef/knife/chop/ui.rb', line 82

def method_missing(name, *args, &block)
  msg = "#{self.class.name}: Method missing: #{name}"
  @logger.fatal(msg)
  raise ChopInternalError.new(msg)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



27
28
29
# File 'lib/chef/knife/chop/ui.rb', line 27

def logger
  @logger
end

Instance Method Details

#err(message) ⇒ Object



63
64
65
# File 'lib/chef/knife/chop/ui.rb', line 63

def err(message)
  error(message)
end

#error(message) ⇒ Object

Print an error message



73
74
75
# File 'lib/chef/knife/chop/ui.rb', line 73

def error(message)
  msg(message)
end

#fatal(message) ⇒ Object

Print a message describing a fatal error.



78
79
80
# File 'lib/chef/knife/chop/ui.rb', line 78

def fatal(message)
  msg(message)
end

#info(message) ⇒ Object

def define_ui_methods()

class << self
  ::Logging::LEVELS.each{|name,level|
      code = <<-CODE
      def #{name}(str)
        msg(str)
      end
      CODE
      self.class.class_eval(code,__FILE__,__LINE__)
  }
end

end



55
56
57
# File 'lib/chef/knife/chop/ui.rb', line 55

def info(message)
  msg(message)
end

#msg(message) ⇒ Object

def define_log_methods( ui )



36
37
38
39
40
41
# File 'lib/chef/knife/chop/ui.rb', line 36

def msg(message)
  caller = Kernel.caller[0]
  match = caller.match(%r/([-\.\/\(\)\w]+):(\d+)(?::in `(\w+)')?/o)
  name = shifted(match[3])
  @logger.send(name, message)
end

#shifted(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chef/knife/chop/ui.rb', line 88

def shifted(name)
  name = 'info' unless ::Logging::LEVELS.has_key?(name)
  num = ::Logging::LEVELS[name]+1
  case name
  when 'todo'
    'error'
  when 'err'
    'error'
  # when 'info'
  #   'debug'
  when 'debug'
    'trace'
  else
    name
  end
end

#step(message) ⇒ Object



59
60
61
# File 'lib/chef/knife/chop/ui.rb', line 59

def step(message)
  msg(message)
end

#warn(message) ⇒ Object

Print a warning message



68
69
70
# File 'lib/chef/knife/chop/ui.rb', line 68

def warn(message)
  msg(message)
end