Skip to content

Instantly share code, notes, and snippets.

@just3ws
Last active October 13, 2017 18:32
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 just3ws/996a7aa07b7142728d9d9f41deede3dc to your computer and use it in GitHub Desktop.
Save just3ws/996a7aa07b7142728d9d9f41deede3dc to your computer and use it in GitHub Desktop.
Blocks that know where their towel is.
# Rails.configuration.permit_automated_emails = true
class Floodwall
def initialize(context, bypass = false, **data, &action)
@context = context
@data = OpenStruct.new(data || {})
@permit = Rails.configuration.permit_automated_emails || bypass || false
Rails.logger.warn { 'Floodwall has been bypassed and will allow emails through' } if bypass
process(action)
end
private
def permitted?
@permit
end
attr_reader :context
attr_reader :permit, :data
def gate
return if permitted?
raise FloodwallBreachedError
end
def process(action)
gate
Rails.logger.debug { 'Floodwall is permitting emails' }
mod = Module.new
mod.send(:define_method, :try_send, action)
extend(mod)
end
end
class FloodwallBreachedError < StandardError
def initialize(msg: 'Floodwall was breached')
super(msg)
end
end
=begin
Floodwall.new(self) do
MyAppMailer.
delay.
emailering('foo', 123)
end.try_send
=end
=begin
Floodwall.new(self) do
MyAppMailer.
delay.
processering_emailinator
end.try_send
=end
=begin
# This mailer was being invoked from a nested module that references the
# parent class. That's kind of an awkward situation to metaprogram for
# without going down a rabbithole. Just added note to expose the instance
# variable via an attr. In the meantime reference the class variable manually.
Floodwall.new(self) do
thing = context.instance_variable_get(:@thing)
MyAppMailer.
delay.
processering(thing)
end.try_send
=end
=begin
Floodwall.new(self) do
::SomeUpstreamConst.
delay(queue: 'optimus_prime').
public_send(template, *context.mailer[:params])
end.try_send if success? # Don't try to send unless necessary
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment