Skip to content

Instantly share code, notes, and snippets.

@zacheryph
Last active August 29, 2015 13:56
Show Gist options
  • Save zacheryph/9334880 to your computer and use it in GitHub Desktop.
Save zacheryph/9334880 to your computer and use it in GitHub Desktop.
Interactor pattern in CoffeeScript
# Interactor pattern
# idea pulled blatently from: https://github.com/collectiveidea/interactor
#
# NEED TO DO:
# - add bluebird, so ALL perform() calls are promises.
# - 'rollback' support to crawl 'backwards' when there is an error
# - 'fail()' for the rollback
#
# Question:
# - Is doing a pattern like this in JS useless?
# - suggestions on doing it better?
# - does this translate well to being useful with vanilla JS?
class Interactor
constructor: (context) ->
@context = context
@perform: (context) ->
instance = new this(context)
instance.perform.call(instance)
instance
class InteractorOrganizer extends Interactor
@perform: (ctx) ->
for inter in this.organize
temp = inter.perform(ctx)
ctx = temp.context
class UserSignin extends Interactor
perform: ->
@context.logged_in = true
console.log("testing #{@context.user} + #{@smash()}")
smash: ->
"see me now"
class StoreSession extends Interactor
perform: ->
console.log "stuff stored to session: #{JSON.stringify(@context)}"
class ProcessSignin extends InteractorOrganizer
@organize: [
UserSignin,
StoreSession
]
# Example Usage:
#
UserSignin.perform({user: "MY USER"})
# testing MY USER + see me now
ProcessSignin.perform({user: "Organize User"})
# testing Organize User + see me now
# stuff stored to session: {"user":"Organize User","logged_in":true}
@rthbound
Copy link

rthbound commented Mar 3, 2014

First!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment