lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Jan 21, 2009 at 5:51 PM, Tomas Guisasola Gorham
<tomas@tecgraf.puc-rio.br> wrote:
>        Hi Bertrand
>
>> module('connection', package.seeall)
>>
>> function new(driver)
>>  local db = {
>>   driver = driver -- luasql driver type
>>  }
>>  -- loads the SQL adapter for the given driver
>>  local adapter = require("adapter." .. driver)
>>  for k, v in pairs(adapter) do
>>   if k ~= "_M" then
>>     db[k] = v
>>   end
>>  end
>>
>>  setmetatable(db, {__index = _M})
>>  return db
>> end
>
>        Why don't you simply:
>
> function new(driver)
>  local db = {
>    driver = driver -- luasql driver type
>  }
>  -- loads the SQL adapter for the given driver
>  local adapter = require("adapter." .. driver)
>  return setmetatable(db, {__index = function(t, k) return _M[k] or
> adapter[k] end})
> end

Hi Tomás,

Thank you for that, I didn't think about it :)
Actually, I also used this :

function new(driver)
  local db = {
    driver = driver -- luasql driver type
  }
  -- loads the SQL adapter for the given driver
  db.adapter = require("db.frigo.adapter." .. driver)
  setmetatable(db, {__index = _M})
  return db
end

But it became quickly boring to check in every function if it is
defined or not in the adapter :) I still need to get used to the Lua
way, less code, more efficiency.



-----
Bertrand Mansion
Mamasam