-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cereal.js
125 lines (111 loc) · 4.07 KB
/
Cereal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var Promise = require('bluebird')
module.exports = {
findSerializerName: function(config) {
if (config.serializer != null && config.each_serializer != null) {
sails.log.debug('Cannot specify serializer: ' + config.serializer + ' and each_serializer: ' + config.each_serializer +' in a unique cerealizer.')
return null
}
var name;
if (config.serializer != null) {
name = config.serializer;
} else if (config.each_serializer != null) {
name = config.each_serializer;
} else {
sails.log.debug('Must define a serializer in the config.')
}
return name;
},
buildBaseAttributes: function(object, attributes) {
_.each(attributes, function(att) { object[att] = null })
return object
},
createInitialJson: function(records, serializer) {
var self = this
var json = {}
// var template_record = this.build.template_record
// var non_attribute_injections = this.getNonAttributeInjections(this.config, template_record)
var attributes = serializer.build.attributes
if (_.isArray(records)) {
json[serializer.identity] = _.map(records, function(record) {
object = { id: record.id }
_.each(attributes, function(att) {
object[att] = null
// var test = self.getConfigValue(serializer.config, att)
// console.log('Test')
// console.log(test[0])
// if (_.isFunction(test[0])) {
// if (self.isPromise(test[0](record))) {
// test[0](record).then(function(res) {
// object[att] = res[Object.keys(res)[0]]
// })
// } else {
// object[att] = test[0](record)
// // Promise.promisifyAll(test[0](record)).then(function(res) {
// // console.log('after prom')
// // console.log(res)
// // object[att] = res
// // })
// }
// }
})
return object
})
}
else {
json[serializer.identity] = this.buildBaseAttributes({ id: records.id }, attributes)
}
return json
},
getConfigValue: function(config, name) {
var val = _.chain(config)
.map(function(conf) {
if (conf.hasOwnProperty(name)) {
return conf[name]
} else {
return null
}
})
.compact()
.flatten()
.value()
return val
},
getAttributeInjections: function(config, record) {
var attribute_injections = _.chain(config)
.map(function(conf) {
if ( _.contains(Object.keys(record), Object.keys(conf)[0] ) ) {
return conf
} else {
return null
}
})
.compact()
.flatten()
.value()
return attribute_injections
},
getNonAttributeInjections: function(config, record) {
var blacklist = ['attributes', 'has_many', 'has_one']
blacklist += Object.keys(record)
var non_attribute_injections = _.chain(config)
.map(function(conf) {
if ( !_.contains(blacklist, Object.keys(conf)[0] ) ) {
return conf
} else {
return null
}
})
.compact()
.flatten()
.value()
return non_attribute_injections
},
getTemplateRecord: function(records) {
if (_.isArray(records)) {
return records[0]
} else {
return records
}
},
isPromise: function(object) { return object instanceof Object && _.contains(object.toString(), 'Promise'); }
}