Class: Outbox::Messages::Base
- Inherits:
-
Object
- Object
- Outbox::Messages::Base
- Includes:
- Outbox::MessageClients, Outbox::MessageFields
- Defined in:
- lib/outbox/messages/base.rb
Instance Method Summary collapse
-
#audience=(audience) ⇒ Object
Sets the ‘audience’ for this message.
-
#body=(body) ⇒ Object
Sets the ‘body’ for this message.
-
#deliver(audience = nil) ⇒ Object
Validates the current message and delivers the message using the defined client.
-
#initialize(fields = nil, &block) ⇒ Base
constructor
Make a new message.
Methods included from Outbox::MessageFields
#[], #[]=, #fields, #fields=, included, #validate_fields
Methods included from Outbox::MessageClients
Constructor Details
#initialize(fields = nil, &block) ⇒ Base
Make a new message. Every message type can be created using a hash, block, or direct assignment.
= Email.new(
to: '[email protected]',
from: '[email protected]'
)
= Email.new do
to '[email protected]'
from '[email protected]'
end
= Email.new
.to = '[email protected]'
.from = '[email protected]'
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/outbox/messages/base.rb', line 21 def initialize(fields = nil, &block) @fields = {} if self.class.default_client @client = self.class.default_client.dup else @client = nil end self.fields = self.class.defaults if block_given? instance_eval(&block) else self.fields = fields unless fields.nil? end end |
Instance Method Details
#audience=(audience) ⇒ Object
Sets the ‘audience’ for this message. All message types must implement this method. By default, this is an alias for a ‘to’ field if present.
40 41 42 |
# File 'lib/outbox/messages/base.rb', line 40 def audience=(audience) self.to = audience if self.respond_to?(:to=) end |
#body=(body) ⇒ Object
Sets the ‘body’ for this message. All message types must implement this method.
46 47 48 |
# File 'lib/outbox/messages/base.rb', line 46 def body=(body) raise NotImplementedError, 'Subclasses must implement a body= method' end |
#deliver(audience = nil) ⇒ Object
Validates the current message and delivers the message using the defined client.
52 53 54 55 56 |
# File 'lib/outbox/messages/base.rb', line 52 def deliver(audience = nil) self.audience = audience if audience validate_fields client.deliver(self) end |