Class: Person::Person
- Inherits:
-
Object
- Object
- Person::Person
- 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
Instance Attribute Summary collapse
-
#id ⇒ Fixnum
readonly
The id of a person.
-
#name ⇒ String
readonly
The name of a person.
Class Method Summary collapse
-
.create_from_attribute_list(list) ⇒ Object
singleton method to create a Person from an array of strings.
Instance Method Summary collapse
-
#initialize(name = "no_name", id = PersonIDGenerator.generate_new_id) ⇒ Person
constructor
initialization.
-
#to_file ⇒ String
creates an output string for the storage in a file.
-
#to_string ⇒ String
creates an output string with the attributes.
Constructor Details
#initialize(name = "no_name", id = PersonIDGenerator.generate_new_id) ⇒ Person
initialization
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
#id ⇒ Fixnum (readonly)
Returns the id of a person.
19 20 21 |
# File 'lib/entity/person/person.rb', line 19 def id @id end |
#name ⇒ String (readonly)
Returns 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
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_file ⇒ String
creates an output string for the storage in a file. The format serves the output format of the output file
39 40 41 |
# File 'lib/entity/person/person.rb', line 39 def to_file "#{@name};#{@id}" end |
#to_string ⇒ String
creates an output string with the attributes
31 32 33 |
# File 'lib/entity/person/person.rb', line 31 def to_string "Person: #{@name} with ID: #{@id}" end |