local lb = require "loop.base"
local model = require "LuaOrm.Model"
require "LuaOrm.Entity"
require "LuaOrm.Associations.Associations"
require "LuaOrm.Associations.AssociationArray"
local table = require "table"
local print = print

module("LuaOrm.Mapper", package.seeall)

model_methods = {}

--returns new model object, in this case an already existing one
function model_methods:new(modelName, newmodel)
	if (lb.classof(newmodel) == model) then
		newmodel.name = modelName
  	return newmodel
  end
  return nil
end

function settable_event(tabla, key, value)
	if (key == "models" and value ~= nil) then
		--need to verify all associations are against defined models
		--for k, v in pairs(value) do
		--	v:ValidateAssociations(value)
		--end
	end	
	rawset(tabla, key, value);
end

--creates new mapper object
function new(table_prefix, conn)
  local app_model = { table_prefix = table_prefix, conn = conn}
  setmetatable(app_model, 
  											{ __index = model_methods,
  												__newindex = settable_event,	
  											}
  						)
  return app_model
end


