Created
June 9, 2012 05:33
-
-
Save renato-zannon/2899634 to your computer and use it in GitHub Desktop.
Strategy pattern to follow users
This file contains hidden or 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 User < Struct.new(:username, :private) | |
alias_method :private?, :private | |
def follow | |
follow_strategy.call | |
end | |
private | |
def follow_strategy | |
if private? | |
PrivateFollow.new(self) | |
else | |
NormalFollow.new(self) | |
end | |
end | |
end | |
class NormalFollow | |
def initialize(user) | |
@user = user | |
end | |
def call | |
# actually_follow code | |
end | |
end | |
class PrivateFollow < NormalFollow | |
def call | |
super if confirm_follow | |
end | |
private | |
def confirm_follow | |
# actual code | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment