Skip to content

Instantly share code, notes, and snippets.

@yukas
Created July 28, 2016 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukas/d19e62258cb89384a6dfc018694c9865 to your computer and use it in GitHub Desktop.
Save yukas/d19e62258cb89384a6dfc018694c9865 to your computer and use it in GitHub Desktop.
Example of a good service object
class UserCreator
attr_reader :email, :first_name, :last_name
attr_reader :created_user
def initialize(email, first_name, last_name)
@email = email
@first_name = first_name
@last_name = last_name
raise ArgumentError, "Missing email" if email.nil?
end
def create_user
check_user_existance
save_user
end
def user_created?
created_user.present?
end
private
def check_user_existance
raise "User already exists" if User.exists?(...)
end
def save_user
@created_user = User.create(...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment