Tim Uckun wrote:
> I have a table about an entity and there are a LOT of options about
> this entitiy. For example isLeftHanded? isRedHaired? doesLikeRedRoses?
> doesHatePolenta? etc.  What is the best way to keep these kinds of
> binary information about an entity? Is there any harm in just adding
> binary fields? Should I use a large number field and OR all the time
> (does limit the number of options per field and makes for messy SQL
> though). Should I have a text field with delimited data in it? Should
> I join a separate table?

Do you want a class to represent such an entity?

   class Entity
     def initialize
       @opts = {:left_handed => false, :red_haired => true}
     end

     def method_missing(name, *args)
       key = name.to_s[0..-2].to_sym
       case name.to_s
       when /\?$/
         @opts[key]
       when /=$/
         @opts[key] = args.first
       when /!$/
         @opts[key] = !@opts[key]
       else
         super
       end
     end
   end

   entity = Entity.new
   entity.red_haired?         => true
   entity.red_haired!
   entity.red_haired?         => false
   entity.left_handed?        => false
   entity.left_handed = true
   entity.left_handed?        => true

Furthermore, you could make the class smarter by allowing the options to 
be symbols that reference other options, i.e.

   # `right_handed' will be the opposite of
   # `left_handed'.
   entity.right_handed = :left_handed!

   entity.female = :male!

   # `hippie' will have the same value
   # as `long_haired'
   entity.hippie = :long_haired

And perhaps even allow procs:

   entity.hippie = proc{|opts| opts[:long_haired] and
                               opts[:likes_hendrix]}

You could use #respond_to? to determine the type of the value.


Cheers,
Daniel