[Kansas-general] API Thoughts

Daniel Berger djberge at qwest.com
Fri May 28 13:13:29 EDT 2004


More musings from Dan...

I was tinkering around with the map_table method.  What do you think of
something like this approach?  There's a little bit of pseudo-code here.

class KSDatabase
   ...
   def map_table(table,klass)
      klass_const = self.class.const_set(klass,Class.new(self.class))
      klass_const.instance_eval{ @table_name = table }
      klass_const.extend Generic_Sql_Handler # (or whatever)
      # For example...
      class << klass_const
         def table_name
            @table_name
         end

         def columns(view='user')
            get_columns(table,view)
            # Set instance variables for each column name
         end
         ...
      end
   end
end

Thus, you could do something like this:

k = KSDatabase.new(...)
k.map_table("some_table","SomeTable")
p KSDatabase::SomeTable.columns # -> "col1,col2,etc"
p KSDatabase::SomeTable.table_name # -> "some_table"

# And, if we define initialize properly...
st1 = KSDatabase::SomeTable.new # -> st is result of "select * from
some_table"
st2 = KSDatabase::SomeTable.new{ |t| t.col1 > 1000 } # subset of
some_table

# Return a result object (or whatever)
obj = KSDatabase.select(st1,st2){
   st1.col1 == st2.col1
}

I guess part of what I'm trying to accomplish is the ability to store
information about the table as either class methods (as I've done), or
alternatively as instance methods.  I kinda like this syntax a bit
better, but then I haven't thought 3 steps ahead to see if this would be
more painful for other operations.

Anyway, just thought I'd toss it out there.

Regards,

Dan

PS - Please give me a map_table alias for the 'table' method. :)


More information about the Kansas-general mailing list