Mailer.deliver do
from "[email protected]"
to "[email protected]"
subject "Threading and Forking"
body "Some content"
end
This code can be implemented like it's shown below.
class Mailer
def self.deliver(&block)
mail = MailBuilder.new(&block).mail
mail.send_mail
end
Mail = Struct.new(:from, :to, :subject, :body) do
def send_mail
puts "Email from: #{from}"
puts "Email to : #{to}"
puts "Subject : #{subject}"
puts "Body : #{body}"
end
end
class MailBuilder
def initialize(&block)
@mail = Mail.new
instance_eval(&block) #Execute passed block in this context
end
attr_reader :mail
%w(from to subject body).each do |m|
define_method(m) do |val|
@mail.send("#{m}=", val)
end
end
end
end