Skip to content

Instantly share code, notes, and snippets.

@kurko
Forked from lastk/gist:2407666
Created April 17, 2012 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kurko/2407768 to your computer and use it in GitHub Desktop.
Save kurko/2407768 to your computer and use it in GitHub Desktop.
# A controller
class PostsController < ActionController::Base
def create
@context = PostManagementContext.new(params[:posts])
if @context.save
# redirect
end
end
def search
# ...
end
# ...
end
# Context class
#
# Could be Context::PostManagement. Whatever now.
class PostManagementContext
def initialize(params)
@params = params
end
def save
@post = Post.new(@params)
if @post.save
# @post is probably ActiveRecord, so there's nothing to extend
@twitter = login_twitter
@twitter.extend TwitterPoster
@twitter.post(@post.body)
end
end
def save_comment
@comment = Comment.new(@params)
# ... instantiates Model
if @comment.save
@twitter = login_twitter
@twitter.extend TwitterFollower
@twitter.follow(@comment.twitter_nickname)
end
end
private
def login_twitter
Twitter.new(login: "login", password: "password")
end
end
# Data object
class Twitter
def initialize(user)
@twitter = Twitter::API.login(user[:login], user[:password]) # fake API class
end
end
# Roles
module TwitterPoster
def post(body)
# calls Twitter API
end
def parse_tweet_body
# ...
end
# other methods related to posting on Twitter
end
module TwitterFollower
def follow(body)
# calls Twitter API
end
# other methods related to following on Twitter
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment