Class: Daneel::User

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

Overview

Represents a generic user. Doesn’t have a room, because multiple rooms can contain the same user. The user’s unique identifier is supplied by the adapter, and only needs to be unique within the context of that adapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, data = nil) ⇒ User

Returns a new instance of User.



10
11
12
13
14
15
16
17
18
19
# File 'lib/daneel/user.rb', line 10

def initialize(id, name, data = nil)
  @id, @name, @data = id, name, data

  # First, try to get initials from the upper-case letters
  @initials = name.gsub(/\P{Upper}/,'')
  # If that fails, just go with the first letter of each word
  @initials = name.gsub(/(?<!^|\s)./,'') if @initials.empty?
  # Short name is just the bit up to the first space
  @short_name = name.match(/^(\S+)/)[0]
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/daneel/user.rb', line 8

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/daneel/user.rb', line 7

def id
  @id
end

#initialsObject

Returns the value of attribute initials.



8
9
10
# File 'lib/daneel/user.rb', line 8

def initials
  @initials
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/daneel/user.rb', line 7

def name
  @name
end

#short_nameObject

Returns the value of attribute short_name.



8
9
10
# File 'lib/daneel/user.rb', line 8

def short_name
  @short_name
end

Instance Method Details

#to_sObject



21
22
23
# File 'lib/daneel/user.rb', line 21

def to_s
  [short_name, short_name.downcase, initials].sample
end