class Module def attr_bool(*ids) for id in ids module_eval <<-"end_eval" attr_reader #{id.inspect} def #{id.to_s}=(value) @#{id.to_s} = Misc.toBool(value) end end_eval end end # # This function processes the ruleset and creates an attribute for each rule # in it. For example, if it finds a rule of TYPE_INTEGER that points to # [:score,SC_TIME_BONUS], it module_evals "attr_accessor :score". # # For rules with non-null numEntities, it also creates numEntities helpers # that return length of the array pointed to by propertyId. # # When this function is used, microchunk rulesets themselves shape the class # (in a way), because they define what class attributes exist. I like that: # one of the reasons to create microchunks originally was to repeat code # less. This reduces it even more, because even class declaration is made # from microchunk rulesets. # def mcAccessorsFromRuleset(ruleset) # IMPORTANT TODO: maybe provide an opt-out mechanism attributesAlreadyDone = Hash.new # to prevent multiple :score entries and similar situations ruleset.each do |rule| propertyId = rule[0] next if !propertyId.is_a? Array || propertyId.length < 1 dataType = (propertyId[0].is_a? Symbol || !propertyId[0] ? TYPE_INTEGER : propertyId[0]) attribute = (propertyId[0].is_a? Symbol ? propertyId[0] : propertyId[1]) next if attributesAlreadyDone[attribute] attributesAlreadyDone[attribute] = true module_eval (dataType == TYPE_BOOLEAN ? "attr_bool" : "attr_accessor") + " :#{attribute}" end end end