Class: Person

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

Overview

Represents a person at work.

!attribute [r] handle

@return [String] The person's handle (username)

!attribute [r] name

@return [String] The person's full name

!attribute [r] email

@return [String, nil] The person's email address, can be nil

!attribute [r] team

@return [String, nil] The team the person belongs to, can be nil

!attribute [r] notes

@return [Array<String>] An array of notes about the person

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle, name, email, team, notes = []) ⇒ Person

Returns a new instance of Person.



18
19
20
21
22
23
24
# File 'lib/person.rb', line 18

def initialize(handle, name, email, team, notes = [])
  @handle = handle
  @name = name
  @email = email
  @team = team
  @notes = notes
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



16
17
18
# File 'lib/person.rb', line 16

def email
  @email
end

#handleObject (readonly)

Returns the value of attribute handle.



16
17
18
# File 'lib/person.rb', line 16

def handle
  @handle
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/person.rb', line 16

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



16
17
18
# File 'lib/person.rb', line 16

def notes
  @notes
end

#teamObject (readonly)

Returns the value of attribute team.



16
17
18
# File 'lib/person.rb', line 16

def team
  @team
end

Class Method Details

.from_hash(hash) ⇒ Person

Creates a new Person instance from a hash of attributes.

Parameters:

  • hash (Hash)

    A hash containing person attributes

Options Hash (hash):

  • :handle (String)

    The person’s handle (username)

  • :name (String)

    The person’s full name

  • :email (String, nil)

    The person’s email address, can be nil

  • :team (String, nil)

    The team the person belongs to, can be nil

  • :notes (Array<String>)

    An array of notes about the person

Returns:

  • (Person)

    A new Person instance

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/person.rb', line 34

def self.from_hash(hash)
  raise ArgumentError, 'Person handle is required' unless hash[:handle] || hash['handle']
  raise ArgumentError, 'Person name is required' unless hash[:name] || hash['name']

  handle = hash[:handle] || hash['handle']
  name = hash[:name] || hash['name']
  email = hash[:email] || hash['email']
  team = hash[:team] || hash['team']
  notes = hash[:notes] || hash['notes'] || []
  Person.new(handle, name, email, team, notes)
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
56
# File 'lib/person.rb', line 52

def ==(other)
  return false unless other.is_a?(Person)

  handle == other.handle && name == other.name && email == other.email && team == other.team && notes == other.notes
end

#to_sObject



46
47
48
49
50
# File 'lib/person.rb', line 46

def to_s
  return "#{name} (~#{handle})" if @email.nil?

  "#{name} (~#{handle}) <#{email}>"
end