Class: IRC::Prefix

Inherits:
Object
  • Object
show all
Defined in:
lib/irc/prefix.rb

Overview

Public: Represents a Message. This class is intended to be subclassed. Its `.parse` and `#valid?` methods are not implemented and should be redefined.

.parse should take a String and return a three-element Array. #valid? should return true or false.

Direct Known Subclasses

RFC2812::Prefix

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_prefix, user = nil, host = nil) ⇒ Prefix

Public: Initializes the prefix. If both `user` and `host` are `nil`, assumes `raw_prefix` to be a prefix String (e.g. `“[email protected]”`).

raw_prefix - A raw prefix String or a nickname String. user - A username String (default: nil). host - A hostname String (default: nil).

Examples

IRC::Prefix.new("[email protected]")
IRC::Prefix.new("nick", "user", "host.com")

31
32
33
34
35
36
37
38
39
# File 'lib/irc/prefix.rb', line 31

def initialize(raw_prefix, user = nil, host = nil)
  if user.nil? && host.nil?
    @raw = raw_prefix
    @nick, @user, @host = self.class.parse(raw_prefix)
  else
    @nick, @user, @host = raw_prefix, user, host
    @raw = to_s
  end
end

Instance Attribute Details

#hostObject (readonly)

Public: Gets the host String.


18
19
20
# File 'lib/irc/prefix.rb', line 18

def host
  @host
end

#nickObject (readonly)

Public: Gets the nick String.


12
13
14
# File 'lib/irc/prefix.rb', line 12

def nick
  @nick
end

#rawObject (readonly)

Public: Gets the raw message String.


9
10
11
# File 'lib/irc/prefix.rb', line 9

def raw
  @raw
end

#userObject (readonly)

Public: Gets the user String.


15
16
17
# File 'lib/irc/prefix.rb', line 15

def user
  @user
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Public: Checks the equality of the prefix and other. The comparison is case-insensitive. Wildcards are ignored.


43
44
45
46
47
48
49
# File 'lib/irc/prefix.rb', line 43

def ==(other)
  unless other.respond_to? :to_str
    raise TypeError, "no implicit conversion of #{other.class} into String"
  end

  finnish_downcase(self.to_s) == finnish_downcase(other.to_str)
end

#=~(other) ⇒ Object

Public: Checks the equality of the prefix and other. The comparison is case-insensitive. Wildcards are honored.


53
54
55
# File 'lib/irc/prefix.rb', line 53

def =~(other)
  self.to_regexp =~ other
end

#hashObject


59
60
61
# File 'lib/irc/prefix.rb', line 59

def hash
  self.to_s.hash
end

#to_regexpObject


63
64
65
66
67
68
# File 'lib/irc/prefix.rb', line 63

def to_regexp
  regexp_string = finnish_case_insensitivity(to_s)
  regexp_string.gsub! /[?*\.]/, "?" => "\\S", "*" => "\\S*", "." => "\\."

  Regexp.new "^#{regexp_string}$", Regexp::IGNORECASE
end

#to_sObject Also known as: to_str


70
71
72
# File 'lib/irc/prefix.rb', line 70

def to_s
  "#{@nick || "*"}!#{@user || "*"}@#{@host || "*"}"
end