require("ember-data/core");
require("ember-data/system/adapter");

var get = Ember.get;

DS.FixtureAdapter = DS.Adapter.extend({

  simulateRemoteResponse: true,

  latency: 50,

  /*
    Implement this method in order to provide data associated with a type
  */
  fixturesForType: function(type) {
    if (type.FIXTURES) {
      var fixtures = Ember.A(type.FIXTURES);
      return fixtures.map(function(fixture){
        if(!fixture.id){
          throw new Error('the id property must be defined for fixture %@'.fmt(fixture));
        }
        fixture.id = fixture.id + '';
        return fixture;
      });
    }
    return null;
  },

  /*
    Implement this method in order to query fixtures data
  */
  queryFixtures: function(fixtures, query, type) {
    return fixtures;
  },

  /*
    Implement this method in order to provide provide json for CRUD methods
  */
  mockJSON: function(type, record) {
    return this.serialize(record, { includeId: true });
  },

  /*
    Adapter methods
  */
  generateIdForRecord: function(store, record) {
    return Ember.guidFor(record);
  },

  find: function(store, type, id) {
    var fixtures = this.fixturesForType(type);

    Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);

    if (fixtures) {
      fixtures = Ember.A(fixtures).findProperty('id', id);
    }

    if (fixtures) {
      this.simulateRemoteCall(function() {
        store.load(type, fixtures);
      }, store, type);
    }
  },

  findMany: function(store, type, ids) {
    var fixtures = this.fixturesForType(type);

    Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);

    if (fixtures) {
      fixtures = fixtures.filter(function(item) {
        return ids.indexOf(item.id) !== -1;
      });
    }

    if (fixtures) {
      this.simulateRemoteCall(function() {
        store.loadMany(type, fixtures);
      }, store, type);
    }
  },

  findAll: function(store, type) {
    var fixtures = this.fixturesForType(type);

    Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);

    this.simulateRemoteCall(function() {
      store.loadMany(type, fixtures);
      store.didUpdateAll(type);
    }, store, type);
  },

  findQuery: function(store, type, query, array) {
    var fixtures = this.fixturesForType(type);

    Ember.assert("Unable to find fixtures for model type "+type.toString(), !!fixtures);

    fixtures = this.queryFixtures(fixtures, query, type);

    if (fixtures) {
      this.simulateRemoteCall(function() {
        array.load(fixtures);
      }, store, type);
    }
  },

  createRecord: function(store, type, record) {
    var fixture = this.mockJSON(type, record);

    fixture.id = this.generateIdForRecord(store, record);

    this.simulateRemoteCall(function() {
      store.didSaveRecord(record, fixture);
    }, store, type, record);
  },

  updateRecord: function(store, type, record) {
    var fixture = this.mockJSON(type, record);

    this.simulateRemoteCall(function() {
      store.didSaveRecord(record, fixture);
    }, store, type, record);
  },

  deleteRecord: function(store, type, record) {
    this.simulateRemoteCall(function() {
      store.didSaveRecord(record);
    }, store, type, record);
  },

  /*
    @private
  */
  simulateRemoteCall: function(callback, store, type, record) {
    function response() {
      Ember.run(callback);
    }

    if (get(this, 'simulateRemoteResponse')) {
      setTimeout(response, get(this, 'latency'));
    } else {
      response();
    }
  }
});

@@ -58,16 +58,70 @@
 apter%22);
+%0Arequire('ember-data/serializers/fixture_serializer');
 %0A%0Avar ge
@@ -226,16 +226,53 @@
 y: 50,%0A%0A
+  serializer: DS.FixtureSerializer,%0A%0A
   /*%0A   
@@ -1247,32 +1247,49 @@
 resForType(type)
+,%0A        fixture
 ;%0A%0A    Ember.ass
@@ -1390,33 +1390,32 @@
  %7B%0A      fixture
-s
  = Ember.A(fixtu
@@ -1457,33 +1457,32 @@
 %0A    if (fixture
-s
 ) %7B%0A      this.s
@@ -1524,19 +1524,34 @@
     
-store.load(
+this.didFindRecord(store, 
 type
@@ -1551,33 +1551,36 @@
 e, type, fixture
-s
+, id
 );%0A      %7D, stor
@@ -1571,35 +1571,28 @@
 );%0A      %7D, 
-store, type
+this
 );%0A    %7D%0A  %7D
@@ -1971,39 +1971,48 @@
 ) %7B%0A        
-store.loadMany(
+this.didFindMany(store, 
 type, fixtur
@@ -2021,35 +2021,28 @@
 );%0A      %7D, 
-store, type
+this
 );%0A    %7D%0A  %7D
@@ -2268,90 +2268,59 @@
     
-store.loadMany(type, fixtures);%0A      store.didUpdateAll(type);%0A    %7D, store, type
+this.didFindAll(store, type, fixtures);%0A    %7D, this
 );%0A 
@@ -2647,27 +2647,47 @@
     
-array.load(
+this.didFindQuery(store, type, 
 fixtures
 );%0A 
@@ -2674,32 +2674,39 @@
 , type, fixtures
+, array
 );%0A      %7D, stor
@@ -2701,27 +2701,20 @@
      %7D, 
-store, type
+this
 );%0A    %7D
@@ -2922,36 +2922,50 @@
 %7B%0A      
-store.didSaveRecord(
+this.didCreateRecord(store, type, 
 record, 
@@ -2981,35 +2981,20 @@
 %0A    %7D, 
-store, type, record
+this
 );%0A  %7D,%0A
@@ -3137,36 +3137,50 @@
 %7B%0A      
-store.didSaveRecord(
+this.didUpdateRecord(store, type, 
 record, 
@@ -3196,35 +3196,20 @@
 %0A    %7D, 
-store, type, record
+this
 );%0A  %7D,%0A
@@ -3308,44 +3308,29 @@
     
-store.didSaveRecord(record);%0A    %7D, 
+this.didDeleteRecord(
 stor
@@ -3339,32 +3339,46 @@
  type, record);%0A
+    %7D, this);%0A
   %7D,%0A%0A  /*%0A    @
@@ -3431,35 +3431,23 @@
 llback, 
-store, type, record
+context
 ) %7B%0A    
@@ -3484,16 +3484,25 @@
 ber.run(
+context, 
 callback

use adapter interface in fixture adapter