From ced2b8ee75cec4fa5cd398712bed7b28b8380b66 Mon Sep 17 00:00:00 2001 From: Juan Date: Thu, 18 Jul 2019 16:59:11 -0300 Subject: [PATCH] Added client propagaion with getInstance --- CHANGELOG.md | 4 ++++ README.md | 2 +- lib/active-client.js | 12 ++++++++++++ package.json | 4 ++-- tests/active-client-test.js | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5bf114..7a5e088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.2.0] - 2019-07-18 +### Added +- `getInstance` with client propagation + ## [1.1.1] - 2019-07-15 ### Changed - `model` package instead of `model-controller` diff --git a/README.md b/README.md index 49e7c3c..c61a2e6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/janis-commerce/active-client.svg?branch=master)](https://travis-ci.org/janis-commerce/active-client) [![Coverage Status](https://coveralls.io/repos/github/janis-commerce/active-client/badge.svg?branch=master)](https://coveralls.io/github/janis-commerce/active-client?branch=master) -**ActiveClient** is a simple module for getting a client from a field and a value. +**ActiveClient** is a simple module for getting an Active Client from a field and a value. The module only has one method and a Model for the get. ## Installation diff --git a/lib/active-client.js b/lib/active-client.js index f74106c..d1eb889 100644 --- a/lib/active-client.js +++ b/lib/active-client.js @@ -12,9 +12,21 @@ class ActiveClient { const client = clients && clients.length === 1 ? clients[0] : false; + if(client) + client.getInstance = this.getInstance(client); + return client; } + static getInstance(client) { + return TheClass => { + const instance = new TheClass(); + instance.client = client; + + return instance; + }; + } + } module.exports = ActiveClient; diff --git a/package.json b/package.json index 00f1bc6..7a1f4ba 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@janiscommerce/active-client", - "version": "1.1.1", - "description": "", + "version": "1.2.0", + "description": "A simple module for getting an Active Client from a field and a value", "main": "index.js", "scripts": { "test": "export TEST_ENV=true; mocha --exit -R nyan --recursive tests/", diff --git a/tests/active-client-test.js b/tests/active-client-test.js index d38a411..3eb41bf 100644 --- a/tests/active-client-test.js +++ b/tests/active-client-test.js @@ -14,6 +14,10 @@ describe('ActiveClient', () => { foo: 'bar' }; + class RandomClass { + + } + afterEach(() => { sandbox.restore(); }); @@ -48,4 +52,18 @@ describe('ActiveClient', () => { assert.deepEqual(client, { ...theClient }); }); + it('should propagate client when getting an instance from a client', async () => { + + sandbox.stub(ModelClient.prototype, 'get') + .returns([theClient]); + + const client = await ActiveClient.getByField('foo', 'bar'); + + assert.equal(typeof client.getInstance, 'function'); + + const instance = client.getInstance(RandomClass); + + assert.deepEqual(client, instance.client); + }); + });