Skip to content

Commit

Permalink
Initial decorators implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 11, 2015
1 parent 426ad5f commit cc14f1a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions dist/lib/Decorators.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/lib/Decorators.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Model from './lib/Model';
import Instance from './lib/Instance';
export {Core, Model, Instance};

export * from './lib/Decorators';

export * from './lib/Plugins';
export * from './lib/Schema';
export * from './lib/Cache';
Expand Down
31 changes: 31 additions & 0 deletions lib/Decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// <reference path="../_references.d.ts" />
import MongoDB = require('mongodb');

import Instance from './Instance';
import {Index, IndexSpecification} from './Index';

export function Index(spec: IndexSpecification, options?: MongoDB.IndexOptions) {
return function(target: typeof Instance) {
target.indexes = target.indexes || [];

if (options) target.indexes.push(<Index>{ spec: spec, options: options });
else target.indexes.push(<Index>{ spec: spec });
}
}

export function Identifier(fromDB: (value: any) => any, toDB: (value: any) => any) {
return function(target: typeof Instance) {
target.identifier = {
apply: fromDB,
reverse: toDB
};
}
}

export function Validate(type: any) {
return function(target: typeof Instance, name: string, descriptor: any) {
descriptor.validateType = type;

return descriptor;
}
}
45 changes: 45 additions & 0 deletions test/Decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// <reference path="../_references.d.ts" />
import * as Iridium from '../index';

interface TestDocument {
_id?: string;
name: string;
email: string;
}

@Iridium.Identifier(x => x / 10, x => x * 10)
@Iridium.Index({ name: 1 })
@Iridium.Index({ email: 1 }, { background: true })
class Test extends Iridium.Instance<TestDocument, Test> implements TestDocument {
_id: string;
name: string;
email: string;
}

describe("Decorators", () => {
describe("Index", () => {
it("should populate the constructor's indexes property with index objects", () => {
chai.expect(Test.indexes).to.exist.and.have.length(2);
});

it("should support just spec indexes", () => {
chai.expect(Test.indexes).to.containOneLike({ spec: { name: 1 } });
});

it("should support indexes with both a spec and options", () => {
chai.expect(Test.indexes).to.containOneLike({ spec: { email: 1 }, options: { background: true }});
});
});

describe("Identifier", () => {
it("should populate the constructor's identifier object", () => {
chai.expect(Test.identifier).to.exist.and.have.property('apply').which.is.a('function');
chai.expect(Test.identifier).to.exist.and.have.property('reverse').which.is.a('function');
});

it("should pass along the correct functions", () => {
chai.expect(Test.identifier.apply(10)).to.eql(1);
chai.expect(Test.identifier.reverse(10)).to.eql(100);
});
});
});

0 comments on commit cc14f1a

Please sign in to comment.