Class: MandrillQueue::Mailer
- Inherits:
-
Object
- Object
- MandrillQueue::Mailer
- Defined in:
- lib/mandrill_queue/mailer.rb
Constant Summary collapse
- ACCESSORS =
End Singleton
[:template, :send_at]
Class Method Summary collapse
- .action_methods ⇒ Object
- .all_templates ⇒ Object
- .configuration ⇒ Object
- .defaults(&block) ⇒ Object
- .defaults=(hash) ⇒ Object
- .message_defaults ⇒ Object
- .method_missing(method, *args) ⇒ Object
- .respond_to?(method, include_private = false) ⇒ Boolean
- .template_prefix(*args) ⇒ Object
Instance Method Summary collapse
- #adapter_options(options = nil) ⇒ Object
- #deliver(options = {}) ⇒ Object
-
#initialize(values = nil) ⇒ Mailer
constructor
A new instance of Mailer.
- #message(&block) ⇒ Object
- #queue ⇒ Object
- #reset! ⇒ Object
- #set!(hash) ⇒ Object
- #to_hash(options = {}) ⇒ Object
- #use_defaults! ⇒ Object
- #validate! ⇒ Object
- #worker_class ⇒ Object
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. || {} end |
Class Method Details
.action_methods ⇒ Object
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_templates ⇒ Object
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 |
.configuration ⇒ Object
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_defaults ⇒ Object
52 53 54 55 56 |
# File 'lib/mandrill_queue/mailer.rb', line 52 def md = configuration. || {} 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.. mailer else super end end |
.respond_to?(method, include_private = false) ⇒ 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 ( = nil) @_adapter_options.merge!() unless .nil? @_adapter_options ||= {} end |
#deliver(options = {}) ⇒ Object
135 136 137 138 |
# File 'lib/mandrill_queue/mailer.rb', line 135 def deliver( = {}) validate! MandrillQueue.adapter.enqueue_to(queue, worker_class, .merge(), to_hash) end |
#message(&block) ⇒ Object
96 97 98 99 100 |
# File 'lib/mandrill_queue/mailer.rb', line 96 def (&block) @_message ||= Message::Internal.new(self.class.) @_message.dsl(&block) if block_given? block_given? ? self : @_message end |
#queue ⇒ Object
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 .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( = {}) hash = {} ACCESSORS.each do |key| value = instance_variable_get("@#{key}".to_sym) hash[key] = value if [:include_nil] || !value.nil? end hash[:message] = .to_hash() rescue nil if !@_message.nil? || [:include_nils] hash[:content] = content.to_key_value_array() rescue nil if !@_content.nil? || [: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 = [] .validate(errors) unless @_message.nil? raise MandrillValidationError.new(errors) unless errors.empty? self end |
#worker_class ⇒ Object
117 118 119 |
# File 'lib/mandrill_queue/mailer.rb', line 117 def worker_class self.class.configuration.default_worker_class || ::MandrillQueue::Worker end |