My proposed solution:
class Criterion class << self attr_reader :criteria attr_reader :requiring_levels end @criteria = [] @requiring_levels = {} def self.abstract_class? true end def self.criteria_for_level( level ) criteria = [] Criterion.criteria.each do |klass| criteria << klass.new( level ) if klass.should_apply?( level ) end return criteria.empty? ? nil : criteria end def self.inherited(subclass) Criterion.criteria << subclass super end end # This subclass exemplifies how new criteria can be added. # The completed? method can be as simple of complicated as you want # and you can define more level specific attributes in the # @requiring_levels hash_map class XPCriterion < Criterion attr_reader :threshold @requiring_levels = {1 => 5, 2 => 10, 3 => 20} def self.should_apply?( level ) self.requiring_levels.has_key? level end def initialize( level ) @threshold = self.class.requiring_levels[ level ] end def completed?( args ) return args > self.threshold end end class Level < ActiveRecord::Base # RoR related stuff omitted def completed?( args ) criteria = Criterion.criteria_for_level( self.number ) return nil unless criteria criteria.each do |c| print ( "Checking #{c.class.name} criteria\n" ) return false unless c.completed?( args ) end return true end end
I like to get up early to go out and breathe fresh air. I feel that it is good for health and a good habit
ReplyDelete19216811ll.com
I agree with everyone else, I wasn't quite sure what it was when i first saw it. But after reading this I'm willing to try it! It sounds interesting....
ReplyDeleteRun4.co gswitch3.co Superfighters.co
Check over here
ReplyDelete