Class: TaskJuggler::Message

Inherits:
Object show all
Includes:
Term::ANSIColor
Defined in:
lib/taskjuggler/MessageHandler.rb

Overview

The Message object can store several classes of messages that the application can send out.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, id, message, sourceFileInfo, line, data, scenario) ⇒ Message

Create a new Message object. The type specifies what tpye of message this is. The following types are supported: fatal, error, warning, info and debug. id is a String that must uniquely identify the source of the Message. message is a String with the actual message. sourceLineInfo is a SourceLineInfo object that can reference a location in a specific file. line is a String of that file. data can be any context sensitive data. sceneario specifies the Scenario in which the message originated.



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
70
71
72
73
74
# File 'lib/taskjuggler/MessageHandler.rb', line 44

def initialize(type, id, message, sourceFileInfo, line, data, scenario)
  unless [ :fatal, :error, :warning, :info, :debug ].
         include?(type)
    raise "Unknown message type: #{type}"
  end
  @type = type

  @id = id

  if message && !message.is_a?(String)
    raise "String object expected as message but got #{message.class}"
  end
  @message = message

  if sourceFileInfo && !sourceFileInfo.is_a?(TextParser::SourceFileInfo)
    raise "SourceFileInfo object expected but got #{sourceFileInfo.class}"
  end
  @sourceFileInfo = sourceFileInfo

  if line && !line.is_a?(String)
    raise "String object expected as line but got #{line.class}"
  end
  @line = line

  @data = data

  if scenario && !scenario.is_a?(Scenario)
    raise "Scenario object expected by got #{scenario.class}"
  end
  @scenario = scenario
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



33
34
35
# File 'lib/taskjuggler/MessageHandler.rb', line 33

def id
  @id
end

#lineObject (readonly)

Returns the value of attribute line.



33
34
35
# File 'lib/taskjuggler/MessageHandler.rb', line 33

def line
  @line
end

#messageObject (readonly)

Returns the value of attribute message.



33
34
35
# File 'lib/taskjuggler/MessageHandler.rb', line 33

def message
  @message
end

#sourceFileInfoObject

Returns the value of attribute sourceFileInfo.



34
35
36
# File 'lib/taskjuggler/MessageHandler.rb', line 34

def sourceFileInfo
  @sourceFileInfo
end

#typeObject (readonly)

Returns the value of attribute type.



33
34
35
# File 'lib/taskjuggler/MessageHandler.rb', line 33

def type
  @type
end

Instance Method Details

#to_logObject

Convert the Message into a String that can be stored in a log file.



96
97
98
99
100
101
102
103
104
105
# File 'lib/taskjuggler/MessageHandler.rb', line 96

def to_log
  str = ""
  # The SourceFileInfo is printed as <fileName>:line:
  if @sourceFileInfo
    str += "#{@sourceFileInfo.fileName}:#{sourceFileInfo.lineNo}: "
  end
  str += "Scenario #{@scenario.id}: " if @scenario
  str += @message
  str
end

#to_sObject

Convert the Message into a String that can be printed to the console.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/taskjuggler/MessageHandler.rb', line 77

def to_s
  str = ""
  # The SourceFileInfo is printed as <fileName>:line:
  if @sourceFileInfo
    str += "#{@sourceFileInfo.fileName}:#{sourceFileInfo.lineNo}: "
  end
  if @scenario
    tag = "#{@type.to_s.capitalize} in scenario #{@scenario.id}: "
  else
    tag = "#{@type.to_s.capitalize}: "
  end
  colors = { :fatal => red, :error => red, :warning => magenta,
             :info => blue, :debug => green }
  str += colors[@type] + tag + @message + reset
  str += "\n" + @line if @line
  str
end