Commit d71a03f

mo khan <mo@mokhan.ca>
2017-01-14 17:39:05
create repository class.
1 parent 9462131
app/domain/__tests__/repository_spec.js
@@ -0,0 +1,23 @@
+import Repository from '../repository';
+
+describe("Repository", () => {
+  let subject = null;
+  const type = 'Account'
+
+  beforeEach(() => {
+    subject = new Repository();
+  });
+
+  afterEach(() => {
+    subject.deleteAll(type);
+  });
+
+  it ("can save an account", () => {
+    subject.save(type, {
+      authentication_token: 'secret'
+    })
+
+    expect(subject.count(type)).toEqual(1);
+    expect(subject.all(type)[0]['authentication_token']).toEqual('secret');
+  });
+});
app/domain/__tests__/user_spec.js
@@ -0,0 +1,35 @@
+class User {
+  constructor(bodyWeight: 0) {
+    this.bodyWeight = bodyWeight;
+  }
+
+  switchTo(program) { }
+
+  nextWorkoutFor(routine) {
+    return new Workout(this.bodyWeight);
+  }
+}
+
+class StrongliftsProgram {
+  routine(name) {
+  }
+}
+
+class Workout {
+  constructor(bodyWeight = 0) {
+    this.bodyWeight = bodyWeight;
+  }
+}
+
+describe("User", () => {
+  it ("creates the next workout", function() {
+    const program = new StrongliftsProgram();
+    const user = new User(240);
+    const routineA = program.routine("A")
+
+    user.switchTo(program)
+    const workout = user.nextWorkoutFor(routineA);
+
+    expect(workout.bodyWeight).toEqual(240);
+  });
+});
app/domain/account.js
@@ -0,0 +1,8 @@
+export default class Account {
+}
+Account.schema = {
+  name: 'Account',
+  properties: {
+    authentication_token: 'string'
+  }
+};
app/domain/repository.js
@@ -0,0 +1,30 @@
+import Realm from 'realm';
+import Account from './account';
+
+export default class Repository {
+  constructor() {
+    this.realm = new Realm({
+      schema: [Account]
+    });
+  }
+
+  all(type) {
+    return this.realm.objects(type);
+  }
+
+  count(type) {
+    return this.all(type).length;
+  }
+
+  save(type, attributes) {
+    this.realm.write(() => {
+      this.realm.create(type, attributes);
+    });
+  }
+
+  deleteAll(type){
+    this.realm.write(() => {
+      this.realm.delete(this.all(type));
+    });
+  }
+}
app/domain/user.js
.gitignore
@@ -42,3 +42,5 @@ android/app/libs
 *.keystore
 
 .env
+*.realm
+*.realm.lock