(function() {

  $READY_NEW = 'READY_NEW';

  $READY_CLEAN = 'READY_CLEAN';

  $READY_DIRTY = 'READY_DIRTY';

  $READY_LAZY = 'READY_LAZY';

  $READY = [$READY_NEW, $READY_CLEAN, $READY_DIRTY, $READY_LAZY];

  $DESTROYED = 'DESTROYED';

  $DESTROYED_CLEAN = 'DESTROYED_CLEAN';

  $DESTROYED_DIRTY = 'DESTROYED_DIRTY';

  $INITIALIZING = 'INITIALIZING';

  $BUSY = 'BUSY';

  $ERROR = 'ERROR';

  $EMPTY = 'EMPTY';

  $LOADING = 'LOADING';

  this.def_class('RedisAdapter',DObject,function(__scope,__class){

    this.__ctor__ = function RedisAdapter(db, options) {
      if (options == null) options = {};
      this["@db"] = db;
      this["@path"] = options['path'];
      this["@cache"] = {};
      return this;
    }; this.__ctor__.prototype = this.prototype;;

    this.prototype["get!"] = function(path, data, block) {
      var body, call;
      var _this = this;
      path += "?debug=1";
      body = data && "[" + __scope.JSON.stringify(data) + "]" || "";
      call = new (__ctor(__scope.XMLHttpRequest))();
      call.onreadystatechange = function() {
        var reply;
        if (call.readyState === 4 && block) {
          reply = __scope.JSON.parse(call.responseText);
          return block(reply.ERROR, reply);
        }
      };
      if (body.length > 50) {
        call.open('POST', path, true);
        call.send(body);
      } else {
        if (data) path += "&pars=" + body;
        call.open('GET', path, true);
        call.send(null);
      }
      return this;
    }; __method_added(this,"get!");

    this.prototype.fetch = function(doc, block) {
      var _this = this;
      this["get!"]([this["@path"], doc.ns(), 'fetch', doc.id()].join("/"), nil, function(err, res) {
        return block && block.call(doc, err, res);
      });
      return this;
    }; __method_added(this,"fetch");

    this.prototype.save = function(doc, block) {
      var _this = this;
      if (doc["new?"]()) {
        this.p('calling create on server with attributes', doc.attributes());
        this["get!"]([this["@path"], doc.ns(), 'create'].join("/"), doc.attributes(), function(err, res) {
          doc["id="](res['_id']);
          return block && block.call(doc, err, res);
        });
      } else {
        this["get!"]([this["@path"], doc.ns(), 'update', doc.id()].join("/"), doc.changes(), function(err, res) {
          doc["@dirty"] = [];
          return block && block.call(doc, err, res);
        });
      }
      return this;
    }; __method_added(this,"save");

    this.prototype.destroy = function(doc, block) {
      var _this = this;
      return this["get!"]([this["@path"], doc.ns(), 'destroy', doc.id()].join("/"), null, function(err, res) {
        block && block.call(doc, err, res);
        return _this.p('doc was destroyed!!!');
      });
    }; __method_added(this,"destroy");

    this.prototype.cache = function(doc, block) {
      var cache, _name;
      if (doc.id()) {
        cache = this["@cache"][_name = doc.class().name()] || (this["@cache"][_name] = {});
        cache[doc.id()] = doc;
      }
      return doc;
    }; __method_added(this,"cache");

    this.prototype["cached?"] = function(klass, id) {
      var cache;
      if (cache = this["@cache"][klass.name()]) return cache[id];
    }; __method_added(this,"cached?");

  });

  this.def_class('Rome',DObject,function(__scope,__class){

    this.repository = function(name) {
      if (name == null) name = 'default';
      return this[name];
    };__singleton_method_added(this,"repository");

    this.__ctor__ = function Rome(options) {
      this["@adapter"] = new (__ctor(__scope.RedisAdapter))(this, options);
      this["@cache"] = {};
      this["@store"] = {};
      __scope.Rome['default'] = this;
      return this;
    }; this.__ctor__.prototype = this.prototype;;

    this.prototype.adapter = function() {
      return this["@adapter"];
    }; __method_added(this,"adapter");

    this.prototype["cached?"] = function(klass, id) {
      return this["@adapter"]["cached?"](klass, id);
    }; __method_added(this,"cached?");

    this.prototype.cache = function(doc, block) {
      return this["@adapter"].cache(doc, block);
    }; __method_added(this,"cache");

    this.prototype.fetch = function(doc, block) {
      return this["@adapter"].fetch(doc, block);
    }; __method_added(this,"fetch");

    this.prototype.save = function(doc, block) {
      return this["@adapter"].save(doc, block);
    }; __method_added(this,"save");

    this.prototype.destroy = function(doc, block) {
      return this["@adapter"].destroy(doc, block);
    }; __method_added(this,"destroy");

  });

}).call($$base);

