Last active
January 1, 2016 00:39
-
-
Save the-undefined/8068187 to your computer and use it in GitHub Desktop.
Creating a DSL inspired by the rspec assertions but with a conversion method thrown in for added semantic measure.
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
module Invite | |
module_function | |
def Invite(people) | |
Invitees.new(people) | |
end | |
class Invitees | |
attr_reader :recipients | |
def initialize people | |
@recipients = people.to_a | |
end | |
def to(event) | |
recipients.each do |recipient| | |
InvitationMailer.rsvp(event, recipient).deliver | |
end | |
end | |
end | |
end | |
# You could use the long format: | |
Invite::Invitess.new(people).to(event) | |
# But the DSL is clearer | |
Invite(people).to(event) | |
# INTEGRATION POINT | |
class InvitationsController < ApplicationController | |
include Invite | |
# ... | |
def create | |
invitees = invitables.where(id: params[:invite_member_ids]) | |
event = events.first_by(id: params[:event_id]) | |
Invite(invitees).to(event) | |
redirect_to event | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment