Friday, October 12, 2012

RoR Double Clutch

This is a pattern have implemented in three different projects of mine, so I'm writting it down once and for all the next times I wrap my head arround this until I remember I have already have done it.

In the Dezquare project, the users are offered a game that helps them in finding a sutable designer. The game varies accorging to all sorts of conditions. The sequence is broken down into a series of basic elements that can be manipulated and tweeked from an admin consule.

The aim is to have an implementor grabbed from the database and react to a particular context as part of a user flow sequance





The players:

  • A controller
  • A context object (as an ActiveRecord)
  • A referenced implementor descriptor (as an ActiveRecord)
  • An implementor

The sequence:

  1. The controller identifies the context object and passes the request params
  2. The context object finds its implementor descriptor and passes itself as an argument
  3. The implementor descriptor converts its implementor class name field into an entity (imp.camelize.constantize)
  4. The implementor descriptor invokes on the implementor method with the context object and its own agruments field (if applicable)

The code 

Note: this code will not work by itself. its only expressing the idea
The standard controller answering a play request
def play
    @game=Game.find(session[:game_id])
    @results = @game.set(params)
end

The context object (Game), persistant and stateful, belongs to a "stage" and passes the request and context
def set(params)
    self.stage.set(self,params)
end

The implementor descriptor (Stage), persisted common object, known here as the "stage" generated an entity and calls it.
def klass
    self.imp.camelize.constantize
end

def set(game,params)
    klass.set(game,params,self.arguments)
end

The implementor (name derived from a data field), unpersisted, statless entity, acts upon the context it is given and returns the results
def self.set(game,params,arguments)
    #pay dirt
end




No comments:

Post a Comment

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.