Skip to content

Instantly share code, notes, and snippets.

@the-undefined
Last active January 1, 2016 00:39
Show Gist options
  • Save the-undefined/8068187 to your computer and use it in GitHub Desktop.
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.
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