Class: SSCBot::ChatLog::Message

Inherits:
Object
  • Object
show all
Extended by:
AttrBool::Ext
Defined in:
lib/ssc.bot/chat_log/message.rb

Overview

The base class of all parsed messages from a chat log file.

Author:

  • Jonathan Bradley Whited

Since:

  • 0.1.0

Constant Summary collapse

TYPES =

Valid types of messages.

You can add your own custom type(s) that you parse manually:

SSCBot::ChatLog::Message.add_type(:custom)

Since:

  • 0.1.0

Set.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, type:) ⇒ Message

Returns a new instance of Message.

Parameters:

  • line (String)

    the raw (unparsed) line from the file

  • type (Symbol)

    what type of message this is; must be one of TYPES

Raises:

  • (ArgumentError)

Since:

  • 0.1.0



76
77
78
79
80
81
82
83
84
# File 'lib/ssc.bot/chat_log/message.rb', line 76

def initialize(line,type:)
  type = type.to_sym

  raise ArgumentError,"invalid line{#{line.inspect}}" if line.nil?
  raise ArgumentError,"invalid type{#{type.inspect}}" if !self.class.valid_type?(type)

  @line = line
  @type = type
end

Instance Attribute Details

#lineString (readonly)

Returns the raw (unparsed) line from the file.

Returns:

  • (String)

    the raw (unparsed) line from the file

Since:

  • 0.1.0



71
72
73
# File 'lib/ssc.bot/chat_log/message.rb', line 71

def line
  @line
end

#typeSymbol (readonly)

Returns what type of message this is; one of TYPES.

Returns:

  • (Symbol)

    what type of message this is; one of TYPES

Since:

  • 0.1.0



72
73
74
# File 'lib/ssc.bot/chat_log/message.rb', line 72

def type
  @type
end

Class Method Details

.add_type(type) ⇒ Object

Adds type to the list of valid TYPES and creates a boolean method for it ending with a ?.

Parameters:

  • type (Symbol, String)

    the new type to add

Since:

  • 0.1.0



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ssc.bot/chat_log/message.rb', line 30

def self.add_type(type)
  type = type.to_sym

  return if TYPES.include?(type)

  TYPES.add(type)

  name = type.to_s.sub('?','q_')

  define_method(:"type_#{name}?") do
    return @type == type
  end
end

.valid_type?(type) ⇒ Boolean

Returns true if type is one of TYPES, else false.

Parameters:

  • type (Symbol)

    the type to check if valid

Returns:

  • (Boolean)

    true if type is one of TYPES, else false

Since:

  • 0.1.0



67
68
69
# File 'lib/ssc.bot/chat_log/message.rb', line 67

def self.valid_type?(type)
  return TYPES.include?(type)
end

Instance Method Details

#type?(type) ⇒ Boolean

A convenience method for comparing anything that responds to :to_sym():, like String.

Parameters:

  • type (String, Symbol)

    the type to convert & compare against

Returns:

  • (Boolean)

    true if this message is of type type, else false

Since:

  • 0.1.0



91
92
93
# File 'lib/ssc.bot/chat_log/message.rb', line 91

def type?(type)
  return @type == type.to_sym
end