Last active
August 29, 2015 14:27
-
-
Save braidn/baacaea0527b722d25cb to your computer and use it in GitHub Desktop.
Simple Publisher
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
require 'publisher' | |
class CreateGroupEmail | |
include Publisher | |
def initialize(payload) | |
@payload = payload | |
end | |
def call | |
return unless group_user | |
users = build_group(Group.create(user_id: group_user.id, | |
groupname: payload_subject)) | |
publish { |listener| listener.new_group(payload, users.map(&:strip)) } | |
end |
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
# Could be a method in any class, the class wouldn't have to include the publisher | |
def create_group | |
@create_group ||= CreateGroupEmail.new(payload).tap do |publisher| | |
publisher.add_consumer(InformUser.new) | |
end | |
end |
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 InformUser | |
def add_member(payload) | |
AddMemberMailer.notification(payload).deliver | |
end | |
def remove_member(payload) | |
RemoveMemberMailer.notification(payload).deliver | |
end | |
def help_user(payload) | |
HelpUserMailer.notification(payload).deliver | |
end | |
def new_group(payload, users) | |
NewGroupMailer.notification(payload, users).deliver | |
end | |
end |
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
require 'set' | |
module Publisher | |
def publish(&block) | |
consumers.each(&block) | |
end | |
def add_consumer(listener) | |
consumers.add(listener) | |
end | |
def consumers | |
@consumers ||= Set.new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment