Last active
December 23, 2015 09:11
-
-
Save ozgun/0f909e968cb2a07f307e to your computer and use it in GitHub Desktop.
Service & Status 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
class Core::DeleteApp < Core::ServiceBase | |
attr_reader :user, :app | |
def initialize(user, app) | |
@user = user | |
@app = app | |
end | |
# Her service object'te sadece bir tane public method var o da `call` methodu. | |
# `call` methodu bir status object dondurur, bu status object `Error` veya `Success` status object olsun. | |
def call | |
if app.destroy | |
send_email_to_user(user) | |
success(app: app, user: user) | |
else | |
error('Error message', app) | |
end | |
end | |
private | |
def send_email_to_user(user) | |
Notifier.notify_user(user).deliver_later | |
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
module Core | |
module Status | |
class Error | |
attr_reader :error, :data | |
# Initilizes an Error object | |
# | |
# @param error [String] Error message | |
# @param error_data [Object] Object or objects which you need to return along with the error message. | |
def initialize(error, data = nil) | |
@error = error | |
@data = data | |
end | |
def success? | |
false | |
end | |
def errors | |
@error | |
end | |
end | |
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
class Core::ServiceBase | |
protected | |
def error(error_message, error_data = nil) | |
Core::Status::Error.new(error_message, error_data) | |
end | |
def success(data = nil) | |
Core::Status::Success.new(data) | |
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
module Core | |
module Status | |
class Success | |
attr_reader :data | |
def initialize(data) | |
@data = data | |
end | |
def success? | |
true | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment