Class: MandrillQueue::Mailer

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

Constant Summary collapse

ACCESSORS =

End Singleton

[:template, :send_at]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = nil) ⇒ Mailer

Returns a new instance of Mailer.



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

def initialize(values = nil)
	set!(values) unless values.nil?
    @_adapter_options = self.class.configuration.adapter_options || {}
end

Class Method Details

.action_methodsObject



13
14
15
16
17
18
19
# File 'lib/mandrill_queue/mailer.rb', line 13

def action_methods
	klass = self
	klass = klass.superclass until klass == MandrillQueue::Mailer
	return [] if self == klass
	self.public_instance_methods(true) -
		klass.public_instance_methods(true)
end

.all_templatesObject



67
68
69
70
71
# File 'lib/mandrill_queue/mailer.rb', line 67

def all_templates
	action_methods.map do |method|
		template_from_method(method)
	end
end

.configurationObject



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

def configuration
	MandrillQueue.configuration
end

.defaults(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/mandrill_queue/mailer.rb', line 37

def defaults(&block)
	return @_defaults ||= {} unless block_given?

	mailer = new
	@_in_defaults = true
	mailer.instance_eval(&block)
	@_defaults = mailer.to_hash
ensure
	@_in_defaults = false
end

.defaults=(hash) ⇒ Object



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

def defaults=(hash)
	@_defaults = hash
end

.message_defaultsObject



52
53
54
55
56
# File 'lib/mandrill_queue/mailer.rb', line 52

def message_defaults
	md = configuration.message_defaults || {}
	md.merge!(defaults[:message]) unless @_in_defaults || defaults[:message].nil?
	md
end

.method_missing(method, *args) ⇒ Object



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

def method_missing(method, *args)
	if respond_to?(method)
		mailer = new(defaults)
		mailer.send(method, *args)
		mailer.template(template_from_method(method)) if mailer.template.blank? &&
			!mailer.message.content_message?
		mailer
	else
		super
	end
end

.respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mandrill_queue/mailer.rb', line 9

def respond_to?(method, include_private = false)
	super || action_methods.include?(method)
end

.template_prefix(*args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/mandrill_queue/mailer.rb', line 58

def template_prefix(*args)
	@template_prefix = args.first unless args.count == 0
	if @template_prefix.nil?
		"#{self.name.chomp('Mailer').sluggify}-"
	else
		@template_prefix
	end
end

Instance Method Details

#adapter_options(options = nil) ⇒ Object



140
141
142
143
# File 'lib/mandrill_queue/mailer.rb', line 140

def adapter_options(options = nil)
  @_adapter_options.merge!(options) unless options.nil?
  @_adapter_options ||= {}
end

#deliver(options = {}) ⇒ Object



135
136
137
138
# File 'lib/mandrill_queue/mailer.rb', line 135

def deliver(options = {})
	validate!
    MandrillQueue.adapter.enqueue_to(queue, worker_class, adapter_options.merge(options), to_hash)
end

#message(&block) ⇒ Object



96
97
98
99
100
# File 'lib/mandrill_queue/mailer.rb', line 96

def message(&block)
	@_message ||= Message::Internal.new(self.class.message_defaults)
	@_message.dsl(&block) if block_given?
	block_given? ? self : @_message
end

#queueObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mandrill_queue/mailer.rb', line 121

def queue
	@_queue ||= \

	if instance_variable_defined?(:@queue)
		instance_variable_get(:@queue)
	elsif worker_class.instance_variable_defined?(:@queue)
		worker_class.instance_variable_get(:@queue)
	elsif worker_class.respond_to?(:queue)
		worker_class.queue
	else
		self.class.configuration.default_queue || :mailer
	end
end

#reset!Object



87
88
89
90
91
92
93
94
# File 'lib/mandrill_queue/mailer.rb', line 87

def reset!
	ACCESSORS.each do |key|
		instance_variable_set("@#{key}", nil)
	end
	@_message = nil
	@_content = nil
	self
end

#set!(hash) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/mandrill_queue/mailer.rb', line 157

def set!(hash)
	hash.symbolize_keys!
	ACCESSORS.each do |key|
		instance_variable_set("@#{key}", hash[key])
	end

	message.set!(hash[:message]) unless hash[:message].nil?
	content.set!(hash[:content]) unless hash[:content].nil?
	self
end

#to_hash(options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mandrill_queue/mailer.rb', line 145

def to_hash(options = {})
	hash = {}
	ACCESSORS.each do |key|
		value = instance_variable_get("@#{key}".to_sym)
		hash[key] = value if options[:include_nil] || !value.nil?
	end

	hash[:message] = message.to_hash(options) rescue nil if !@_message.nil? || options[:include_nils]
	hash[:content] = content.to_key_value_array(options) rescue nil if !@_content.nil? || options[:include_nils]
    hash
end

#use_defaults!Object



170
171
172
173
# File 'lib/mandrill_queue/mailer.rb', line 170

def use_defaults!
	set!(self.class.defaults) unless self.class.defaults.nil?
	self
end

#validate!Object



175
176
177
178
179
180
181
# File 'lib/mandrill_queue/mailer.rb', line 175

def validate!
	errors = []
	message.validate(errors) unless @_message.nil?

	raise MandrillValidationError.new(errors) unless errors.empty?
	self
end

#worker_classObject



117
118
119
# File 'lib/mandrill_queue/mailer.rb', line 117

def worker_class
	self.class.configuration.default_worker_class || ::MandrillQueue::Worker
end