Class: Ahoy::ContactList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ahoy/contact_list.rb

Overview

Ahoy::ContactList is a self-populating collection of Contacts, and provides methods to retrieve and iterate over its contents.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_name = nil) ⇒ ContactList

:call-seq: ContactList.new(user_name=nil) -> contact_list

Create a new Ahoy::ContactList. Provide a username as the argument to avoid adding our user to the list.



20
21
22
23
24
25
26
27
# File 'lib/ahoy/contact_list.rb', line 20

def initialize(user_name=nil)
  @user_name = user_name
  @list = []
  @weak_list = []
  @lock = Mutex.new
  
  start_browse
end

Instance Attribute Details

#user_nameObject (readonly)

Returns the value of attribute user_name.



12
13
14
# File 'lib/ahoy/contact_list.rb', line 12

def user_name
  @user_name
end

Instance Method Details

#[](name) ⇒ Object Also known as: find_by_name

:call-seq: contact_list -> contact or nil

Returns the first contact who’s fullname or name matches name.

The case equality operator (===) is used in the comparison, so strings or regexps can be used as the argument.



45
46
47
# File 'lib/ahoy/contact_list.rb', line 45

def [](name)
  find {|c| name === c.fullname || name === c.name}
end

#each(&block) ⇒ Object

:call-seq: contact_list.each {|contact| block } -> contact_list

Calls block once for each contact in the contact list.



33
34
35
36
# File 'lib/ahoy/contact_list.rb', line 33

def each(&block)
  lock.synchronize {list.each(&block)}
  self
end

#find_by_ip(ip) ⇒ Object

:call-seq: contact_list.find_by_ip(string) -> contact or nil

Returns the first contact with the ip address matching string.



54
55
56
# File 'lib/ahoy/contact_list.rb', line 54

def find_by_ip(ip)
  find {|contact| contact.ip_addresses.include?(ip)}
end