Skip to content

Commit

Permalink
added some documentation and another validation + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YashdalfTheGray committed Jan 17, 2020
1 parent 296cf36 commit e650827
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/playground/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ module Models
class Contact
attr_reader :id, :created_at, :updated_at, :name, :email, :phone

# Creates a new contact object
#
# @param name [String] the name of the contact
# @param email [String] the email of the contact
# @param phone [String] the phone number of the contact
# @returns [Contact] a new contact with the given information
def initialize(name, email, phone)
@id = SecureRandom.uuid
@created_at = DateTime.now
@updated_at = DateTime.now

validate_email email

@name = name
@email = email
@phone = phone
Expand All @@ -26,7 +35,7 @@ def name=(name)
end

def email=(email)
raise ArgumentError, "invalid email provided: #{email}" if (email =~ URI::MailTo::EMAIL_REGEXP).nil?
validate_email email

@email = email
@updated_at = DateTime.now
Expand All @@ -37,6 +46,12 @@ def phone=(phone)
@updated_at = DateTime.now
end

def validate_email(email)
raise ArgumentError, "invalid email provided: #{email}" if (email =~ URI::MailTo::EMAIL_REGEXP).nil?

true
end

def to_h
instance_variables.each_with_object({}) do |v, hash|
name = v.to_s[1..-1]
Expand Down
5 changes: 5 additions & 0 deletions spec/playground/models/contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ module Models
expect(test_contact.updated_at).to be_a(DateTime)
end

it 'throws an error on an invalid email' do
expect { Contact.new(Faker::Name.name, 'stuff@', Faker::PhoneNumber.cell_phone) }
.to raise_error ArgumentError
end

context 'properties' do
it 'tracks the updated datetime when updating name' do
original_update_time = test_contact.updated_at
Expand Down

0 comments on commit e650827

Please sign in to comment.