-
Notifications
You must be signed in to change notification settings - Fork 74
Mediators
Pedro Belo edited this page Jul 10, 2015
·
2 revisions
Pliny encourages the use of the Mediator pattern to keep your endpoints lean and your models focused.
The basic idea is very simple: move any workflow or interaction between objects into a new class. You want to avoid things like a Sinatra post
block starting a transaction, after_save
hooks sending emails, etc.
Create a new mediator like:
$ pliny-generate mediator artists/creator
created mediator file ./lib/mediators/artists/creator.rb
created test ./spec/mediators/artists/creator_spec.rb
The basic class is trivial:
module Mediators::Artists
class Creator < Mediators::Base
def initialize
end
def call
end
end
end
The only thing Pliny sets in the base mediator class is a shortcut to initialize and call. So you can trigger the code above with:
Mediators::Artists::Creator.run
Needless to say mediators only exist to wrap complex workflows and interactions. Do not create a mediator for every single endpoint in your API – if all you need is to call User.create
then do that directly.
Further reading:
Basics
Diving in
- bin/setup
- Config
- CORS
- Endpoints
- Error Handling
- Logging
- Models
- Mediators
- Migrations
- Rake Tasks
- Request IDs
- RequestStore
- Schema
- Serialization
- Testing
- Updating
Guides