Last active
August 29, 2015 13:56
-
-
Save cpb/9262581 to your computer and use it in GitHub Desktop.
replace method with method object
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 UserAdder < Struct.new(:user_params, :company_id, :role) | |
extend FriendlyMethodObject | |
def initialize(*) | |
super | |
# Provide defaults for optional members | |
self.role ||= :default_role | |
end | |
def call | |
# Proprietary business logic here | |
end | |
private | |
# Put access to non attribute dependencies in private methods | |
def create_user(params) | |
end | |
def company | |
end | |
def send_enrollment_email(user) | |
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 FriendlyMethodObject | |
def call(by_name = {}) | |
new(*by_name.values_at(members)).call | |
end | |
alias_method :perform, :call | |
def to_proc | |
proc do |*args| | |
call(*args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment