Class: Worklog::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
!attribute [r] inactive
@return [Boolean] Whether the person is inactive (for example left the company)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle:, name:, **params) ⇒ Person

Returns a new instance of Person.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/person.rb', line 21

def initialize(handle:, name:, **params)
  # params to symbol keys
  params = params.transform_keys(&:to_sym)

  @handle = handle
  @name = name
  @github_username = params[:github_username]
  @email = params[:email]
  @team = params[:team]
  @notes = params[:notes] || []
  @inactive = params[:inactive] || false
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



19
20
21
# File 'lib/person.rb', line 19

def email
  @email
end

#github_usernameObject (readonly)

Returns the value of attribute github_username.



19
20
21
# File 'lib/person.rb', line 19

def github_username
  @github_username
end

#handleObject (readonly)

Returns the value of attribute handle.



19
20
21
# File 'lib/person.rb', line 19

def handle
  @handle
end

#inactiveObject (readonly)

Returns the value of attribute inactive.



19
20
21
# File 'lib/person.rb', line 19

def inactive
  @inactive
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/person.rb', line 19

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



19
20
21
# File 'lib/person.rb', line 19

def notes
  @notes
end

#teamObject (readonly)

Returns the value of attribute team.



19
20
21
# File 'lib/person.rb', line 19

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)


55
56
57
58
59
60
61
62
63
64
# File 'lib/person.rb', line 55

def self.from_hash(hash)
  hash = hash.transform_keys(&:to_sym)

  raise ArgumentError, 'Person handle is required' unless hash[:handle]
  raise ArgumentError, 'Person name is required' unless hash[:name]

  handle = hash[:handle]
  name = hash[:name]
  Person.new(handle: handle, name: name, **hash)
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
75
76
# File 'lib/person.rb', line 72

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

#active?Boolean

Returns true if the person is active (not inactive). If not specified, persons are active by default.

Returns:

  • (Boolean)

    true if active, false otherwise



37
38
39
# File 'lib/person.rb', line 37

def active?
  !@inactive
end

#inactive?Boolean

Returns true if the person is marked as inactive.

Returns:

  • (Boolean)

    true if inactive, false otherwise



43
44
45
# File 'lib/person.rb', line 43

def inactive?
  @inactive
end

#to_sObject



66
67
68
69
70
# File 'lib/person.rb', line 66

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

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