-
Notifications
You must be signed in to change notification settings - Fork 3
The Guest User
By convention, a Guest User is a User Entity with whatever attributes your UserRepository
says a Guest User has. This will often be mirrored in your User
Entity class itself, so that code can ask a User Entity if it's a Guest User without having to know what defines it as such.
For example:
In lib/your_stuff/entities/user.rb
# frozen_string_literal: true
class User < Hanami::Entity
GUEST_EMAIL = '[email protected]'
GUEST_NAME = 'Guest User'
GUEST_PROFILE = 'This is the Guest User. It can do nothing.'
def guest?
name == GUEST_NAME && email == GUEST_EMAIL && profile == GUEST_PROFILE
end
end
and in lib/your_stuff/repositories/user_repository.rb
# frozen_string_literal: true
require 'securerandom'
class UserRepository < Hanami::Repository
# ... associations, finders, etc ...
def self.guest_user
@guest_user ||= entity.new name: User::GUEST_NAME, email: User::GUEST_EMAIL,
password_hash: SecureRandom.alphanumeric(48),
profile: User::GUEST_PROFILE, id: -1
end
def guest_user
self.class.guest_user
end
end
Note that the Entity's #guest?
method doesn't simply compare against the Repository's .guest_user
returned Entity, as the latter has an unmatchable password_hash
attribute. ("But doesn't this violate SRP", I hear you asking back there? No, because there is still a single source of truth for "what makes a Guest a Guest": the constant definitions on the User
class that are used by the UserRepository
.
Got a better idea? Open an issue or, better yet, a PR and let's talk about it!