Last active
October 13, 2017 18:32
-
-
Save just3ws/996a7aa07b7142728d9d9f41deede3dc to your computer and use it in GitHub Desktop.
Blocks that know where their towel is.
This file contains hidden or 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
# 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 |
This file contains hidden or 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 FloodwallBreachedError < StandardError | |
def initialize(msg: 'Floodwall was breached') | |
super(msg) | |
end | |
end |
This file contains hidden or 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
=begin | |
Floodwall.new(self) do | |
MyAppMailer. | |
delay. | |
emailering('foo', 123) | |
end.try_send | |
=end |
This file contains hidden or 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
=begin | |
Floodwall.new(self) do | |
MyAppMailer. | |
delay. | |
processering_emailinator | |
end.try_send | |
=end |
This file contains hidden or 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
=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 |
This file contains hidden or 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
=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