Created
October 24, 2020 09:59
-
-
Save therod/282e37d558f6dd0d389273ff477977b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
attr_accessor :first_name, :last_name, :birth_year, :car_registration | |
def initialize(first_name, last_name, birth_year, car_registration) | |
@first_name = PersonDataChecker.check_name(first_name) | |
@last_name = PersonDataChecker.check_name(last_name) | |
@birth_year = PersonDataChecker.check_birth_year(birth_year) | |
@car_registration = PersonDataChecker.check_car_registration(car_registration) | |
end | |
end | |
class PersonDataChecker | |
def self.check_name(name) | |
raise "Text field should not be empty" if name.empty? | |
raise "Text should have 3 or more charachters" if name.length < 3 | |
raise "First letter must be uppercase" unless /[[:upper:]]/.match(name) | |
end | |
def self.check_birth_year(year) | |
raise "Birth year must be between 1900 and 2020" unless (1900..2020).to_a.include?(year) | |
end | |
def self.check_car_registration(registration) | |
canton_code = registration[0..1] | |
number_code = registration[2..-1] | |
raise "Registration number must contain two letters at the beginning" unless canton_code.match(/^[[:alpha:][:blank:]]+$/) | |
raise "Registration number must containt at least 6 digits" if number_code.length > 6 | |
end | |
end | |
hans = Person.new("Hans", "Ruedi", 1987, "A333999") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment