Class: Person::Person

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

Overview

This class provides the basis for describing a person entity. When providing an #id it is uses as the identification number. If no value is provided the #id will be created by the PersonIDGenerator. Every child class should overwrite the method #to_string and #to_file to provide a output string for the terminal and the file with all of its attributes. In addition it should implement a method Person.create_from_attribute_list to provide creation of an object via an Array of strings. The method is used, when objects are loaded from a file.

Direct Known Subclasses

Student

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "no_name", id = PersonIDGenerator.generate_new_id) ⇒ Person

initialization

Parameters:

  • name (String) (defaults to: "no_name")

    the name of the person

  • id (Integer) (defaults to: PersonIDGenerator.generate_new_id)

    the unique id of a person



24
25
26
27
# File 'lib/entity/person/person.rb', line 24

def initialize(name="no_name", id=PersonIDGenerator.generate_new_id)
  @id = id
  @name = name
end

Instance Attribute Details

#idFixnum (readonly)

Returns the id of a person.

Returns:

  • (Fixnum)

    the id of a person



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

def id
  @id
end

#nameString (readonly)

Returns the name of a person.

Returns:

  • (String)

    the name of a person



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

def name
  @name
end

Class Method Details

.create_from_attribute_list(list) ⇒ Object

singleton method to create a Person::Person from an array of strings

Parameters:

  • list (Array)

    list of string attributes to create a person

Raises:

  • (ArgumentError)

    if the size of the list does not fit the number of required attributes



47
48
49
50
51
52
53
54
55
# File 'lib/entity/person/person.rb', line 47

def self.create_from_attribute_list(list)
  if (list.size != 2)
    raise ArgumentError,
          " Error: list contains wrong number of arguments to create" \
          " a person.".red
  end
  id = list[1].to_i
  self.new(list[0], id)
end

Instance Method Details

#to_fileString

creates an output string for the storage in a file. The format serves the output format of the output file

Returns:

  • (String)

    a string coding all information of the person for storage

See Also:



39
40
41
# File 'lib/entity/person/person.rb', line 39

def to_file
  "#{@name};#{@id}"
end

#to_stringString

creates an output string with the attributes

Returns:

  • (String)

    output string for this person



31
32
33
# File 'lib/entity/person/person.rb', line 31

def to_string
  "Person: #{@name} with ID: #{@id}"
end