require 'set' module CheckboxSetHelper def self.included(klass) klass.extend(ClassMethods) end module ClassMethods def checkbox_set prop @checkbox_sets ||= [] @checkbox_sets.push(prop.to_s) self.class_eval "def #{prop}_set; @#{prop}_set; end;" self.class_eval "def #{prop}_set= s; @#{prop}_set = s; end;" self.class_eval "def #{Inflector.pluralize(prop)}; self.#{prop}_set.to_a; end;" end def checkbox_sets @checkbox_sets end end def method_missing methodname, *args found = false self.class.checkbox_sets.each do |set_name| self.send(set_name + '_set=', Set.new) if !self.send(set_name + '_set') set = self.send(set_name + '_set') prefix = set_name + '_' if prefix.eql?(methodname.to_s[0..prefix.length-1]) if "=".eql?(methodname.to_s[-1..-1]) if args[0] == "1" set.add(methodname.to_s[prefix.length..-2].to_i) else set.delete(methodname.to_s[prefix.length..-2].to_i) end else return set.include?(methodname.to_s[prefix.length..-1].to_i) end found = true break end end super if !found end end