Class: Cereal::User

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/user.rb

Overview

Synopsis

Cereal::User Represents a user in an IRC channel. This class is used by Cereal::Connection and Cereal::Bot to keep track of who is in each channel the bot currenty occupies. You may receive an array of Users if you request a list of users in a channel from the bot.

@lower_nick is set on initialization (or when @nick is changed) and is simply a downcased version of @nick, stored as an instance variable for performance reasons.

Author

Ross “Raws” Paffett

Copyright © 2009 Ross Paffett. Licensed under the MIT license: www.opensource.org/licenses/mit-license.php

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix, nick) ⇒ User

Constructs a User object with a known prefix and nick.



27
28
29
30
31
# File 'lib/user.rb', line 27

def initialize(prefix, nick)
  @prefix = prefix.strip
  @nick = nick.strip
  @lower_nick = @nick.downcase
end

Instance Attribute Details

#lower_nickObject (readonly)

Returns the value of attribute lower_nick.



24
25
26
# File 'lib/user.rb', line 24

def lower_nick
  @lower_nick
end

#nickObject

Returns the value of attribute nick.



24
25
26
# File 'lib/user.rb', line 24

def nick
  @nick
end

#prefixObject (readonly)

Returns the value of attribute prefix.



24
25
26
# File 'lib/user.rb', line 24

def prefix
  @prefix
end

Instance Method Details

#<=>(other) ⇒ Object

Returns the result of calling <=> on lowercased nicks. This is useful for sorting lists of Users.



88
89
90
91
92
93
94
# File 'lib/user.rb', line 88

def <=>(other)
  if other.is_a? Cereal::User
    other.lower_nick <=> @lower_nick
  else
    other.to_s.downcase! <=> @lower_nick
  end
end

#eql?(other) ⇒ Boolean

Returns true as in ==.

Returns:

  • (Boolean)


77
78
79
# File 'lib/user.rb', line 77

def eql?(other)
  self == other
end

#hashObject

Returns the hash of this User object.



82
83
84
# File 'lib/user.rb', line 82

def hash
  @lower_nick.hash
end

#op(is_op) ⇒ Object

Sets whether or not this user is an operator.



39
40
41
42
43
44
45
# File 'lib/user.rb', line 39

def op(is_op)
  if is_op
    @prefix = voice? ? '@+' : '@'
  else
    @prefix = voice? ? '+' : ''
  end
end

#op?Boolean

Returns whether or not this user is an operator.

Returns:

  • (Boolean)


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

def op?
  !@prefix.index('@').nil?
end

#to_sObject

Returns the nick of the user with their prefix, if any, attached.

u = Cereal::User.new('+', 'Monty')
u.to_s => "+Monty"


66
67
68
# File 'lib/user.rb', line 66

def to_s
  @prefix + @nick
end

#voice(has_voice) ⇒ Object

Sets whether or not this user has voice.



53
54
55
56
57
58
59
# File 'lib/user.rb', line 53

def voice(has_voice)
  if has_voice
    @prefix = op? ? '@+' : '+'
  else
    @prefix = op? ? '@' : ''
  end
end

#voice?Boolean

Returns whether or not this user has voice.

Returns:

  • (Boolean)


48
49
50
# File 'lib/user.rb', line 48

def voice?
  !@prefix.index('+').nil?
end