Class: Lux::Mailer

Inherits:
Object show all
Defined in:
lib/lux/mailer/mailer.rb

Overview

Rails mode via method missing is suported Mailer.confirm_email(‘rejotl@gmailcom’).deliver Mailer.confirm_email(‘rejotl@gmailcom’).body

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMailer

Returns a new instance of Mailer.



47
48
49
# File 'lib/lux/mailer/mailer.rb', line 47

def initialize
  @mail = DynamicClass.new subject: nil, body: nil, to: nil, cc: nil, from: nil
end

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



17
18
19
# File 'lib/lux/mailer/mailer.rb', line 17

def mail
  @mail
end

Class Method Details

.deliverObject



40
41
42
# File 'lib/lux/mailer/mailer.rb', line 40

def deliver
  send(method_name, *args).deliver
end

.method_missing(method_sym, *args) ⇒ Object



36
37
38
# File 'lib/lux/mailer/mailer.rb', line 36

def method_missing method_sym, *args
  prepare(method_sym, *args)
end

.prepare(template, *args) ⇒ Object

Mailer.prepare(:confirm_email, ‘rejotl@gmailcom’)



21
22
23
24
25
26
27
28
29
30
# File 'lib/lux/mailer/mailer.rb', line 21

def prepare template, *args
  obj = new
  obj.instance_variable_set :@_template, template

  ClassCallbacks.execute obj, :before
  obj.send template, *args
  ClassCallbacks.execute obj, :after

  obj
end

.render(method_name, *args) ⇒ Object



32
33
34
# File 'lib/lux/mailer/mailer.rb', line 32

def render method_name, *args
  send(method_name, *args).body
end

Instance Method Details

#bodyObject



74
75
76
77
78
79
80
# File 'lib/lux/mailer/mailer.rb', line 74

def body
  return @mail.body if @mail.body

  helper = Lux::Helper.new self, self.class.helper

  Lux::Template.render_with_layout "mailer/#{@_template}", helper
end

#deliverObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lux/mailer/mailer.rb', line 51

def deliver
  raise "From in mailer not defined"    unless @mail.from
  raise "To in mailer not defined"      unless @mail.to
  raise "Subject in mailer not defined" unless @mail.subject

  require 'mail'

  Mail.defaults { delivery_method Lux.config(:mail).delivery, Lux.config(:mail).opts }

  m = Mail.new
  m[:from]         = @mail.from
  m[:to]           = @mail.to
  m[:subject]      = @mail.subject
  m[:body]         = @mail.body || body
  m[:content_type] = 'text/html; charset=UTF-8'

  Thread.new { m.deliver! }
end

#deliver_laterObject



70
71
72
# File 'lib/lux/mailer/mailer.rb', line 70

def deliver_later
   Lux.delay self, :deliver
end

#subjectObject



82
83
84
# File 'lib/lux/mailer/mailer.rb', line 82

def subject
  @mail.subject
end

#toObject



86
87
88
# File 'lib/lux/mailer/mailer.rb', line 86

def to
  @mail.to
end