Last active
December 18, 2015 00:49
-
-
Save GCorbel/5699834 to your computer and use it in GitHub Desktop.
My way to use services objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in app/roles/ | |
module CollectorRole | |
def paintings | |
["Van Gogh"] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in app/models/ | |
class Message < Struct.new(:title, :message) | |
def to_s | |
title | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in app/services/ | |
class MessageGeocoder < Struct.new(:message) | |
extend Service | |
def call | |
puts "Geocoding of #{message}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in app/services/ | |
class MessageNotifier < Struct.new(:sender, :receiver, :message) | |
extend Service | |
def call | |
receiver.extend(CollectorRole) | |
puts "#{sender} send a message to #{receiver} to have information about #{receiver.paintings}" | |
message = Message.new("Hello", "World") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in app/controllers/ | |
sender = User.new("Bob") | |
receiver = User.new("Arold") | |
notifier = MessageNotifier.new(sender, receiver) | |
notifier.call | |
MessageGeocoder.new(notifier.message).call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in lib/ | |
module Service | |
def self.call(*args) | |
self.new(args).call | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#in app/models | |
class User < Struct.new(:name, :email) | |
def to_s | |
name | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a simple example of what I do in an rails application. This is PORO but it's easily integrable in a Rails project. The controller call services objects which contains all the logic.
I think it's simpler than the DCI paradigm. It use roles too but in a simpler way. My models can stay only data but the controller is not tyrannized by the context.