Class: ActionMailer::Enqueable::Deferred

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer/enqueable/deferred.rb

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Deferred

Returns a new instance of Deferred.



25
26
27
28
# File 'lib/action_mailer/enqueable/deferred.rb', line 25

def initialize(params)
  @params = params
  @params.symbolize_keys!
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'lib/action_mailer/enqueable/deferred.rb', line 23

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



23
24
25
# File 'lib/action_mailer/enqueable/deferred.rb', line 23

def params
  @params
end

Class Method Details

.encoderObject



19
20
21
# File 'lib/action_mailer/enqueable/deferred.rb', line 19

def self.encoder
  RecordEncoder
end

.from_hash(params) ⇒ Object



13
14
15
16
17
# File 'lib/action_mailer/enqueable/deferred.rb', line 13

def self.from_hash(params)
  decoded = encoder.decode(params)

  new(decoded)
end

.from_json(params) ⇒ Object



9
10
11
# File 'lib/action_mailer/enqueable/deferred.rb', line 9

def self.from_json(params)
  from_hash(ActiveSupport::JSON.decode(params))
end

Instance Method Details

#==(other) ⇒ Object



75
76
77
78
# File 'lib/action_mailer/enqueable/deferred.rb', line 75

def ==(other)
  other.respond_to?(:params) &&
    other.params == params
end

#argumentsObject



46
47
48
# File 'lib/action_mailer/enqueable/deferred.rb', line 46

def arguments
  params[:arguments]
end

#attributesObject



50
51
52
# File 'lib/action_mailer/enqueable/deferred.rb', line 50

def attributes
  { :mailer_name => mailer_name, :method_id => method_id, :arguments => arguments }
end

#encodedObject



71
72
73
# File 'lib/action_mailer/enqueable/deferred.rb', line 71

def encoded
  self.class.encoder.encode(params)
end

#mailObject



30
31
32
# File 'lib/action_mailer/enqueable/deferred.rb', line 30

def mail
  @mailer ||= mailer_class.send(method_id, *arguments).create
end

#mailer_classObject



34
35
36
# File 'lib/action_mailer/enqueable/deferred.rb', line 34

def mailer_class
  mailer_name.constantize
end

#mailer_nameObject



38
39
40
# File 'lib/action_mailer/enqueable/deferred.rb', line 38

def mailer_name
  params[:mailer_name]
end

#method_idObject



42
43
44
# File 'lib/action_mailer/enqueable/deferred.rb', line 42

def method_id
  params[:method_id]
end

#to_json(options = {}) ⇒ Object



67
68
69
# File 'lib/action_mailer/enqueable/deferred.rb', line 67

def to_json(options = {})
  ActiveSupport::JSON.encode(encoded)
end

#valid?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/action_mailer/enqueable/deferred.rb', line 58

def valid?
  @errors = attributes.map do |name, value|
    "#{name} can't be nil" if value.nil?
  end
  @errors.compact!

  @errors.empty?
end

#validate!Object



54
55
56
# File 'lib/action_mailer/enqueable/deferred.rb', line 54

def validate!
  valid? || raise(Invalid.new("Deferred mailer is invalid: #{errors.inspect}"))
end