Skip to content

Commit

Permalink
Merge pull request #29 from RomainForks/feature/improvements
Browse files Browse the repository at this point in the history
Feature/improvements
  • Loading branch information
rhwilr authored Jan 22, 2019
2 parents 5d58b13 + 8a9b2c0 commit 30f33ae
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 74 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ You can let bumblebee generate the transformer for you by running:
adonis make:transformer User
```

The class must extend `Adonis/Addons/Bumblebee/TransformerAbstract` and implement at least a `transform` method.
The class must extend `BaseTransformer` and implement at least a `transform` method.

```js
const TransformerAbstract = use('Adonis/Addons/Bumblebee/TransformerAbstract')
const BaseTransformer = use('BaseTransformer')

class UserTransformer extends TransformerAbstract {
class UserTransformer extends BaseTransformer {
transform (model) {
return {
id: model.id,
Expand All @@ -146,6 +146,8 @@ const users = await User.all()
return transform.collection(users, UserTransformer)
```

*Note:* You can also directly use the namespace of the transformer. `transform.collection(users, 'App/Transformers/UserTransformer')`

*Note:* Passing the Transformer as the second argument will terminate the fluent interface. If you want to chain more methods after the call to `collection` or `item` you should only pass the first argument and then use the `transformWith` method to define the transformer. See [Fluent Interface](#fluent-interface)


Expand All @@ -158,7 +160,7 @@ Most of the time our data does not only consist of simple properties on the mode

```js
class BookTransformer extends TransformerAbstract {
defaultInclude () {
static get defaultInclude () {
return [
'author'
]
Expand All @@ -180,9 +182,9 @@ class BookTransformer extends TransformerAbstract {
module.exports = BookTransformer
```

Includes defined in the `defaultInclude` method will always be included in the returned data.
Includes defined in the `defaultInclude` getter will always be included in the returned data.

You have to specify the name of the include by returning an array of all includes from the `defaultInclude` method.
You have to specify the name of the include by returning an array of all includes from the `defaultInclude` getter.
Then you create an additional Method for each include named like in the example: `include{Name}`

The include method returns a new resource, that can either be an `item` or a `collection`. See [Resources](#resources)
Expand All @@ -196,7 +198,7 @@ The include method returns a new resource, that can either be an `item` or a `co

```js
class BookTransformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'author'
]
Expand Down
2 changes: 2 additions & 0 deletions providers/BumblebeeProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BumblebeeProvider extends ServiceProvider {
this.app.bind('Adonis/Addons/Bumblebee', app => {
return Bumblebee
})

this.app.alias('Adonis/Addons/Bumblebee/TransformerAbstract', 'BaseTransformer')
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Bumblebee/Scope.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { ioc } = require('@adonisjs/fold')

const TransformerAbstract = require('./TransformerAbstract')
const Resources = require('./Resources')

Expand Down Expand Up @@ -29,7 +31,7 @@ class Scope {
/**
* Passes the data through the transformers and serializers and returns the transformed data
*/
async toArray () {
async toJSON () {
// run the transformation on the data
let [rawData] = await this._executeResourceTransformers()

Expand Down Expand Up @@ -174,6 +176,11 @@ class Scope {
* @param {*} Transformer
*/
_getTransformerInstance (Transformer) {
// if the transformer is a string, use the IoC to fetch the instance.
if (typeof Transformer === 'string') {
return ioc.use(Transformer)
}

// if the transformer is a class, create a new instance
if (Transformer && Transformer.prototype instanceof TransformerAbstract) {
return new Transformer()
Expand All @@ -199,8 +206,8 @@ class Scope {
* @param {*} Transformer
*/
_transformerHasIncludes (Transformer) {
let defaultInclude = Transformer.defaultInclude()
let availableInclude = Transformer.availableInclude()
let defaultInclude = Transformer.constructor.defaultInclude
let availableInclude = Transformer.constructor.availableInclude

return defaultInclude.length > 0 || availableInclude.length > 0
}
Expand Down
10 changes: 5 additions & 5 deletions src/Bumblebee/TransformerAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class TransformerAbstract {
/*
* Resources that can be included if requested
*/
availableInclude () {
static get availableInclude () {
return []
}

/*
* List of resources to automatically include
*/
defaultInclude () {
static get defaultInclude () {
return []
}

Expand Down Expand Up @@ -82,7 +82,7 @@ class TransformerAbstract {

// if the include uses a resource, run the data through the transformer chain
if (resource instanceof Resources.ResourceAbstract) {
includeData[include] = await this._createChildScopeFor(parentScope, resource, include).toArray()
includeData[include] = await this._createChildScopeFor(parentScope, resource, include).toJSON()
} else {
// otherwise, return the data as is
includeData[include] = resource
Expand Down Expand Up @@ -115,9 +115,9 @@ class TransformerAbstract {
* @param {*} parentScope
*/
_figureOutWhichIncludes (parentScope) {
let includes = this.defaultInclude()
let includes = this.constructor.defaultInclude

let requestedAvailableIncludes = this.availableInclude().filter(i => parentScope._isRequested(i))
let requestedAvailableIncludes = this.constructor.availableInclude.filter(i => parentScope._isRequested(i))

return includes.concat(requestedAvailableIncludes)
}
Expand Down
15 changes: 11 additions & 4 deletions src/Bumblebee/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Bumblebee {

if (transformer) {
this.transformWith(transformer)
return this.toArray()
return this.toJSON()
}

return this
Expand All @@ -76,7 +76,7 @@ class Bumblebee {

if (transformer) {
this.transformWith(transformer)
return this.toArray()
return this.toJSON()
}

return this
Expand Down Expand Up @@ -115,7 +115,7 @@ class Bumblebee {

if (transformer) {
this.transformWith(transformer)
return this.toArray()
return this.toJSON()
}

return this
Expand Down Expand Up @@ -191,7 +191,14 @@ class Bumblebee {
* Terminates the fluid interface and returns the transformed data.
*/
toArray () {
return this._createData().toArray()
return this.toJSON()
}

/**
* Terminates the fluid interface and returns the transformed data.
*/
toJSON () {
return this._createData().toJSON()
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/available-includes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Bumblebee = require('../src/Bumblebee')
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract')

class Book1Transformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'author',
'characters'
Expand All @@ -40,7 +40,7 @@ class Book1Transformer extends TransformerAbstract {
}

class Book2Transformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'author',
'characters'
Expand All @@ -62,7 +62,7 @@ class Book2Transformer extends TransformerAbstract {
}

class Book2CharacterTransformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'actor'
]
Expand Down
12 changes: 6 additions & 6 deletions test/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Bumblebee = require('../src/Bumblebee')
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract')

class IDTransformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'ienv'
]
Expand Down Expand Up @@ -50,7 +50,7 @@ test.group('Context', (group) => {
.item(data)
.transformWith(IDTransformer)
.withContext(ctx)
.toArray()
.toJSON()

assert.equal(transformed.id, 3)
assert.equal(transformed.env, 'testing')
Expand All @@ -67,7 +67,7 @@ test.group('Context', (group) => {
.include('ienv')
.transformWith(IDTransformer)
.withContext(ctx)
.toArray()
.toJSON()

assert.equal(transformed.id, 3)
assert.equal(transformed.env, 'testing')
Expand All @@ -83,7 +83,7 @@ test.group('Context', (group) => {
let transformed = await transform
.item(data)
.transformWith(model => ({ id: model.item_id }))
.toArray()
.toJSON()

assert.equal(transformed.id, 3)
})
Expand All @@ -97,7 +97,7 @@ test.group('Context', (group) => {
let transformed = await transform
.item(data)
.transformWith(IDTransformer)
.toArray()
.toJSON()

assert.equal(transformed.id, 3)
assert.equal(transformed.env, 'testing')
Expand All @@ -119,7 +119,7 @@ test.group('Context', (group) => {
let transformed = await Bumblebee.create()
.item(data)
.transformWith(UserTransformer)
.toArray()
.toJSON()

assert.equal(transformed.id, 42)
})
Expand Down
8 changes: 4 additions & 4 deletions test/detault-includes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Bumblebee = require('../src/Bumblebee')
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract')

class Book1Transformer extends TransformerAbstract {
defaultInclude () {
static get defaultInclude () {
return [
'author'
]
Expand All @@ -35,7 +35,7 @@ class Book1Transformer extends TransformerAbstract {
}

class Book2Transformer extends TransformerAbstract {
defaultInclude () {
static get defaultInclude () {
return [
'author',
'characters',
Expand Down Expand Up @@ -82,7 +82,7 @@ test.group('Default Includes', () => {
let transformed = await Bumblebee.create()
.item(data)
.transformWith(Book1Transformer)
.toArray()
.toJSON()

assert.deepEqual(transformed, {
id: 1,
Expand Down Expand Up @@ -110,7 +110,7 @@ test.group('Default Includes', () => {
let transformed = await Bumblebee.create()
.item(data)
.transformWith(Book2Transformer)
.toArray()
.toJSON()

assert.deepEqual(transformed, {
title: 'Harry Potter and the Chamber of Secrets',
Expand Down
10 changes: 5 additions & 5 deletions test/eagerload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Bumblebee = require('../src/Bumblebee')
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract')

class Book1Transformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'author',
'dragon',
Expand Down Expand Up @@ -83,7 +83,7 @@ test.group('EagerLoading', (group) => {
.item(data)
.transformWith(Book1Transformer)
.include(['author'])
.toArray()
.toJSON()

assert.equal(data.$loadCalled, 1)
assert.deepEqual(transformed, {
Expand All @@ -101,7 +101,7 @@ test.group('EagerLoading', (group) => {
.item(data)
.transformWith(Book1Transformer)
.include(['author', 'characters'])
.toArray()
.toJSON()

assert.equal(data.$loadCalled, 1)
assert.deepEqual(transformed, {
Expand Down Expand Up @@ -135,7 +135,7 @@ test.group('EagerLoading', (group) => {
.item(data)
.transformWith(Book1Transformer)
.include(['author'])
.toArray()
.toJSON()

assert.equal(data.$loadCalled, 1)
assert.deepEqual(transformed, {
Expand All @@ -160,7 +160,7 @@ test.group('EagerLoading', (group) => {
.item(data)
.transformWith(Book1Transformer)
.include(['dragon'])
.toArray()
.toJSON()

assert.equal(data.$loadCalled, 1)
assert.deepEqual(transformed, {
Expand Down
6 changes: 3 additions & 3 deletions test/exception.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Bumblebee = require('../src/Bumblebee')
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract')

class IDTransformer extends TransformerAbstract {
availableInclude () {
static get availableInclude () {
return [
'notexisting'
]
Expand All @@ -40,7 +40,7 @@ test.group('Exception', (group) => {
.item(data)
.include(['notexisting'])
.transformWith(IDTransformer)
.toArray()
.toJSON()
} catch ({ message }) {
assert.equal(message, 'A method called \'includeNotexisting\' could not be found in \'IDTransformer\'')
}
Expand Down Expand Up @@ -70,7 +70,7 @@ test.group('Exception', (group) => {
try {
await Bumblebee.create()
._setData('ResourceAbstract', [{ item_id: 3 }])
.toArray()
.toJSON()
} catch ({ message }) {
assert.equal(message, 'This resourcetype is not supported. Use Item or Collection')
}
Expand Down
Loading

0 comments on commit 30f33ae

Please sign in to comment.