Class: Lux::Mailer

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

Overview

Rails mode via method missing is suported Mailer.email_login(‘[email protected]’).deliver Mailer.email_login(‘[email protected]’).body

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMailer

Returns a new instance of Mailer.



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

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

Instance Attribute Details

#mailObject (readonly)

Returns the value of attribute mail.



20
21
22
# File 'lib/lux/mailer/mailer.rb', line 20

def mail
  @mail
end

Class Method Details

.deliverObject



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

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

.method_missing(method_sym, *args) ⇒ Object



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

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

.prepare(template, *args) ⇒ Object

Mailer.prepare(:email_login, ‘[email protected]’)



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

def prepare template, *args
  obj = new
  obj.instance_variable_set :@_template, template
  Object.class_callback :before, obj
  obj.send template, *args
  Object.class_callback :after, obj
  obj
end

.render(method_name, *args) ⇒ Object



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

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

Instance Method Details

#bodyObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/lux/mailer/mailer.rb', line 69

def body
  data = @mail.body

  unless data
    helper = Lux::View::Helper.new self, self.class.helper
    data = Lux::View.render_with_layout "layouts/#{self.class.layout}", "mailer/#{@_template}", helper
  end

  data.gsub(%r{\shref=(['"])/}) { %[ href=#{$1}#{Lux.config.host}/] }
end

#deliverObject



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

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

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

  Lux.delay { m.deliver! }

  instance_exec m, &Lux.config.on_mail
end

#subjectObject



80
81
82
# File 'lib/lux/mailer/mailer.rb', line 80

def subject
  @mail.subject
end

#toObject



84
85
86
# File 'lib/lux/mailer/mailer.rb', line 84

def to
  @mail.to
end