Reputation: 2872
I am trying to design the points system for a game application, and having some issues figuring out the best way to setup the models. The basic needs are:
Each user can be assigned essentially a "PointsCalculator" object, responsible for keeping track of the points that the user is earning
Each user's "PointsCalculator" logic could potentially be different. For example, one user might earn an extra 100 points every time he plays for more than an hour consecutively. Another user might earn double points between the hours of 8PM and 10PM.
We are trying to figure out the best way to create a model setup that will allow us to store logic that can be customized for each user, but is still DRY and testable.
I don't need anyone to write code for me - I would more just appreciate the feedback of more experienced developers, or anyone who has tackled a similar issue in the past.
Note: Above has been edited for additional clarity, but let me know if it is still unclear.
Upvotes: 2
Views: 418
Reputation: 115541
Difficult question which depends on your complexity level:
If all Points Calculator are well defined, you should head towards Single Table Inheritance (STI). The most simple case.
But if much flexibility is demanded, this becomes harder and you should consider the inclusion of methods dynamically, a la DCI. It's the most complex. See details below.
Ex: you store a list of calculator behaviors in a serialized object within your model:
behaviors = [ 'behavior1', 'behavior2' ]
Each behavior is linked to a class or module with the same name, eg: Behavior1
and Behavior2
.
Then you add behaviors to your object on the fly whenever you need them, for instance in an after_initialize
.
after_initialize :set_behaviors
def set_behaviors
behaviors.each do |b|
self.extend(b.constantize)
end
end
Upvotes: 3