Skip to content

Commit

Permalink
Create shortcut in Mixin for findOne and findAll. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbrown authored Sep 13, 2017
1 parent 98f7b12 commit 04ef632
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG


## v0.2.1
* issue #7: Create shortcut in Mixin for findOne and findAll.


## v0.2.0
* issue #3: Create DynamoDb item marshaler for converting pbj to and from DynamoDb json format.

Expand Down
64 changes: 31 additions & 33 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"jsnext:main": "./index.js",
"dependencies": {
"base-64": "^0.1.0",
"bignumber.js": "^4.0.2"
"bignumber.js": "^4.0.4"
},
"peerDependencies": {
"@gdbots/common": "^0.1.0",
Expand All @@ -42,7 +42,7 @@
"elasticsearch": "^13.0.1",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-config-airbnb-base": "^11.1.3",
"eslint-config-airbnb-base": "^11.3.2",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.9.0",
Expand All @@ -51,7 +51,7 @@
"lodash-es": "^4.17.4",
"md5": "^2.2.1",
"moment": "^2.18.1",
"rimraf": "^2.6.1",
"rimraf": "^2.6.2",
"tape": "^4.6.3",
"utf8": "^2.1.2",
"uuid": "^3.1.0"
Expand Down
49 changes: 49 additions & 0 deletions src/Mixin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable class-methods-use-this */
import LogicException from './exceptions/LogicException';
import MessageResolver from './MessageResolver';

/**
* We store all Mixin instances to accomplish a loose flyweight strategy.
Expand All @@ -26,6 +27,54 @@ export default class Mixin {
return instances.get(this);
}

/**
* Shortcut to resolving a mixin to one concrete schema.
*
* @param {?string} inPackage
* @param {?string} inCategory
*
* @returns {Schema}
*/
static findOne(inPackage = null, inCategory = null) {
return MessageResolver.findOneUsingMixin(this.create(), inPackage, inCategory);
}

/**
* Shortcut to resolving a mixin to one concrete schema.
*
* @param {?string} inPackage
* @param {?string} inCategory
*
* @returns {Schema}
*/
findOne(inPackage = null, inCategory = null) {
return MessageResolver.findOneUsingMixin(this, inPackage, inCategory);
}

/**
* Shortcut to resolving a mixin to all concrete schemas.
*
* @param {?string} inPackage
* @param {?string} inCategory
*
* @returns {Schema[]}
*/
static findAll(inPackage = null, inCategory = null) {
return MessageResolver.findAllUsingMixin(this.create(), inPackage, inCategory);
}

/**
* Shortcut to resolving a mixin to all concrete schemas.
*
* @param {?string} inPackage
* @param {?string} inCategory
*
* @returns {Schema[]}
*/
findAll(inPackage = null, inCategory = null) {
return MessageResolver.findAllUsingMixin(this, inPackage, inCategory);
}

/**
* @returns {SchemaId}
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/Mixin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import test from 'tape';
import Mixin from '../src/Mixin';
import SchemaId from '../src/SchemaId';
import SampleMixinV1 from './fixtures/SampleMixinV1';
import SampleMixinV2 from './fixtures/SampleMixinV2';
import SampleUnusedMixinV1 from './fixtures/SampleUnusedMixinV1';

test('Mixin tests', (t) => {
const mixin = SampleMixinV1.create();
Expand All @@ -21,3 +23,35 @@ test('Mixin tests', (t) => {

t.end();
});


test('Mixin findOne/findAll tests', (t) => {
const mixin1 = SampleMixinV1.create();
const mixin2 = SampleMixinV2.create();
const unusedMixin = SampleUnusedMixinV1.create();

t.same(SampleMixinV1.findAll().length, 2);
t.same(mixin1.findAll().length, 2);

t.same(SampleMixinV2.findOne().getCurie().toString(), 'gdbots:pbj.tests::sample-message');
t.same(mixin2.findOne().getCurie().toString(), 'gdbots:pbj.tests::sample-message');

try {
mixin1.findOne();
t.fail('mixin findOne should not pass when has multiple consumers');
} catch (e) {
t.pass('mixin findOne failed since it has multiple consumers');
}

try {
SampleUnusedMixinV1.findAll();
unusedMixin.findAll();
SampleUnusedMixinV1.findOne();
unusedMixin.findOne();
t.fail('unusedMixin findOne/findAll should not pass when has no consumers');
} catch (e) {
t.pass('mixin findOne/findAll failed since it has no consumers');
}

t.end();
});

0 comments on commit 04ef632

Please sign in to comment.