Skip to content

Commit 42cda16

Browse files
committed
Add blueprint for transforms mixin to use with serializers
1 parent 56fd1c7 commit 42cda16

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

blueprints/addon-import/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ module.exports = {
1414
if (options.pod && options.hasPathToken) {
1515
return blueprintName;
1616
}
17-
var moduleName = options.dasherizedModuleName.replace('jsonapi-', '');
17+
var moduleName;
18+
if (blueprintName.match(/transform-mixin/) !== null) {
19+
moduleName = options.dasherizedModuleName + '-transforms';
20+
} else {
21+
moduleName = options.dasherizedModuleName;
22+
}
1823
return moduleName;
1924
},
2025
__path__: function(options) {
@@ -25,6 +30,8 @@ module.exports = {
2530
var blueprintName = options.originBlueprintName.replace('jsonapi-', '');
2631
if (blueprintName.match(/dictionary/) !== null) {
2732
return 'utils/dictionaries';
33+
} else if (blueprintName.match(/transform-mixin/) !== null) {
34+
return 'mixins';
2835
} else {
2936
return inflector.pluralize(blueprintName);
3037
}
@@ -49,6 +56,8 @@ module.exports = {
4956
}
5057
if (blueprintName.match(/dictionary/) !== null) {
5158
modulePath = [addonName, 'utils/dictionaries', fileName].join('/');
59+
} else if (blueprintName.match(/transform-mixin/) !== null) {
60+
modulePath = [addonName, 'mixins', fileName + '-transforms'].join('/');
5261
}
5362
return {
5463
modulePath: modulePath

blueprints/jsonapi-model-test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var pathUtil = require('ember-cli-path-utils');
88
var getDependencyDepth = require('ember-cli-get-dependency-depth');
99

1010
module.exports = {
11-
description: 'Generates a model unit test.',
11+
description: 'Generates a (ember-jsonapi-resource) model unit test.',
1212

1313
locals: function(options) {
1414
var resource = options.entity.name || options.args[1];

blueprints/jsonapi-serializer-test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var SerializerBlueprint = require('../jsonapi-serializer');
88
var getDependencyDepth = require('ember-cli-get-dependency-depth');
99

1010
module.exports = {
11-
description: 'Ember JSON API Resources: generates a serializer unit test.',
11+
description: 'Generates an (ember-jsonapi-resource) serializer unit test.',
1212

1313
locals: function(options) {
1414
var resource = options.entity.name || options.args[1];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Ember from 'ember';
2+
<%= imports %>
3+
4+
export default Ember.Mixin.create({
5+
6+
<%= methods %>
7+
8+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*jshint node:true*/
2+
var inflector = require('inflection');
3+
var stringUtil = require('ember-cli-string-utils');
4+
var EOL = require('os').EOL;
5+
6+
module.exports = {
7+
description: 'Generates an mixin for using value transforms w/ ember-jsonapi-resources serializers.',
8+
9+
anonymousOptions: ['name', 'attr-name'],
10+
11+
locals: function(options) {
12+
var entity = options.args[1];
13+
var attrNames = Object.keys(options.entity.options);
14+
var attrName, imports = [], methods = [];
15+
16+
for (var i = 0; i < attrNames.length; i++) {
17+
attrName = attrNames[i];
18+
imports.push( makeImport(attrName) );
19+
methods.push( makeTransformMethods(attrName) );
20+
}
21+
22+
return {
23+
imports: imports.join(EOL),
24+
methods: methods.join(',' + EOL + EOL)
25+
};
26+
},
27+
28+
fileMapTokens: function() {
29+
return {
30+
__name__: function (options) {
31+
return options.dasherizedModuleName + '-transforms';
32+
},
33+
__path__: function(options) {
34+
return inflector.pluralize(options.originBlueprintName.replace('jsonapi-transform-', ''));
35+
}
36+
};
37+
}
38+
};
39+
40+
function makeImport(attrName) {
41+
var transformName = makeTransformName(attrName);
42+
var dasherized = stringUtil.dasherize(attrName);
43+
return "import " + transformName + " from '../transforms/" + dasherized + "';";
44+
}
45+
46+
function makeTransformMethods(attrName) {
47+
return [ makeDeserializeMethod(attrName), makeSerializeMethod(attrName) ].join(',' + EOL + EOL);
48+
}
49+
50+
function makeSerializeMethod(attrName) {
51+
var camelized = stringUtil.camelize(attrName);
52+
var methodName = 'serialize'+ stringUtil.classify(attrName) + 'Attribute';
53+
var transformName = makeTransformName(attrName);
54+
var loc = [];
55+
loc.push(' ' + methodName + '(deserialized) {');
56+
loc.push(' ' + 'return ' + transformName + '.serialize(deserialized);');
57+
loc.push(' }');
58+
return loc.join(EOL);
59+
}
60+
61+
function makeDeserializeMethod(attrName) {
62+
var camelized = stringUtil.camelize(attrName);
63+
var methodName = 'deserialize'+ stringUtil.classify(attrName) + 'Attribute';
64+
var transformName = makeTransformName(attrName);
65+
var loc = [];
66+
loc.push(' ' + methodName + '(serialized) {');
67+
loc.push(' ' + 'return ' + transformName + '.deserialize(serialized);');
68+
loc.push(' }');
69+
return loc.join(EOL);
70+
}
71+
72+
function makeTransformName(attrName) {
73+
return stringUtil.camelize(attrName) + 'Transform';
74+
}

0 commit comments

Comments
 (0)