Class: Dreamcatcher::Mailer
- Inherits:
-
Object
- Object
- Dreamcatcher::Mailer
- Defined in:
- lib/dreamcatcher/mailer.rb
Defined Under Namespace
Classes: EmailContext
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #build_email_options(context) ⇒ Object
- #erb_template_file(dir, name, type) ⇒ Object
- #handle_exception(context) ⇒ Object
-
#initialize(configuration) ⇒ Mailer
constructor
A new instance of Mailer.
- #render_body(type, context, options) ⇒ Object
- #render_erb_template(template_file, context, &block) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Mailer
Returns a new instance of Mailer.
9 10 11 |
# File 'lib/dreamcatcher/mailer.rb', line 9 def initialize(configuration) @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
7 8 9 |
# File 'lib/dreamcatcher/mailer.rb', line 7 def configuration @configuration end |
Instance Method Details
#build_email_options(context) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/dreamcatcher/mailer.rb', line 22 def (context) = { :to => configuration.evaluate(:to, context), :from => configuration.evaluate(:from, context), :subject => configuration.evaluate(:subject, context), :via => configuration.evaluate(:via, context), :via_options => configuration.evaluate(:via_options, context) } [:body] = render_body(:text, context, ) [:html_body] = render_body(:html, context, ) .delete_if { |k, v| v.nil? } raise "E-mail has no to recipient!" unless [:to] raise "E-mail has no body!" unless [:body] || [:html_body] end |
#erb_template_file(dir, name, type) ⇒ Object
66 67 68 |
# File 'lib/dreamcatcher/mailer.rb', line 66 def erb_template_file(dir, name, type) File.join(dir, "#{name}.#{type}.erb") end |
#handle_exception(context) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/dreamcatcher/mailer.rb', line 13 def handle_exception(context) = (context) if configuration.evaluate(:deliver, context) Pony.mail() else # TODO: print / log email somewhere end end |
#render_body(type, context, options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dreamcatcher/mailer.rb', line 42 def render_body(type, context, ) template_dir = configuration.evaluate(:template_dir, context) template = configuration.evaluate(:template, context) template_file = erb_template_file(template_dir, template, type) email_context = EmailContext.new(context, ) if File.exists?(template_file) layout_file = erb_template_file(template_dir, 'layout', type) if File.exists?(layout_file) render_erb_template(layout_file, email_context) do render_erb_template(template_file, email_context) end else render_erb_template(template_file, email_context) end else nil end end |
#render_erb_template(template_file, context, &block) ⇒ Object
62 63 64 |
# File 'lib/dreamcatcher/mailer.rb', line 62 def render_erb_template(template_file, context, &block) ERB.new(File.read(template_file), nil, '<>').result(context.get_binding(&block)) end |