Skip to content

Instantly share code, notes, and snippets.

@christopherstyles
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save christopherstyles/2ecc3851e9936e2452e9 to your computer and use it in GitHub Desktop.

Select an option

Save christopherstyles/2ecc3851e9936e2452e9 to your computer and use it in GitHub Desktop.
Service objects, noun-first
# Illustrate the usage of service objects named in the noun-first form.
# a test user
user = User.where(name: 'Oleg').first
UserListSubscription.subscribe(oleg, list)
# => "Oleg was added to the mailing list."
UserListSubscription.unsubscribe(oleg, list)
# => "Oleg was removed from the mailing list."
GoodbyeEmailDelivery.send(oleg)
# => "Sent a goodbye email to Oleg."
UserCommentRemoval.remove(oleg)
# => "Removed comments by Oleg."
InactiveUserArchival.archive([oleg])
# => "Oleg was removed from the 10x10 list."
# => "Sent a goodbye email to Oleg."
# => "Removed comments by Oleg.
# app/services/goodbye_email_delivery.rb
class GoodbyeEmailDelivery
def self.send(user)
# Notifier.goodbye(user).deliver
puts "Sent a goodbye email to #{user.name}."
end
end
# app/services/inactive_user_archival.rb
class InactiveUserArchival
def self.archive(users)
users.each do |user|
UserListSubscription.unsubscribe(user)
GoodbyeEmailDelivery.send(user)
UserCommentRemoval.remove(user)
end
end
end
# app/services/user_comment_removal.rb
class UserCommentRemoval
def self.remove(user)
# user.comments.destroy_all
puts "Removed comments by #{user.name}."
end
end
# app/services/user_list_subscription.rb
class UserListSubscription
def self.subscribe(user, list = MailingList.new)
# list.add!(user.email)
puts "#{user.name} was added to the #{list.name} list."
end
def self.unsubscribe(user, list = MailingList.new)
# list.remove!(user.email)
puts "#{user.name} was removed from the #{list.name} list."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment