Bryce
Bryce

Reputation: 2872

Rails Model for flexible points system

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:

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

Answers (1)

apneadiving
apneadiving

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

Related Questions