Skip to content

Commit 7c6c14d

Browse files
committed
init
1 parent a6a4eee commit 7c6c14d

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
var mongoose = require('mongoose');
4+
5+
var locales=['en','ru'];
6+
7+
(function(){
8+
var ma = mongoose.Schema.prototype.add;
9+
var addI18n=function(obj){
10+
var keys = Object.keys(obj);
11+
var ret={};
12+
13+
for (var i = 0; i < keys.length; ++i) {
14+
var key = keys[i];
15+
var val = obj[key];
16+
17+
if (typeof val != "object") {
18+
ret[key]=val;
19+
continue;
20+
};
21+
22+
var kkeys=Object.keys(val);
23+
var localize=false;
24+
for (var ii=0; ii<kkeys.length;++ii){
25+
var kkey=kkeys[ii];
26+
var vval=val[kkey];
27+
if (typeof vval=="object"){
28+
val[kkey]=addI18n(vval)
29+
}else{
30+
if (vval && kkey=="localize"){
31+
localize=true;
32+
}
33+
}
34+
}
35+
if (localize){
36+
delete(val.localize);
37+
var nval={};
38+
for (var j=0;j<locales.length;j++){
39+
nval[locales[j]]=val;// TODO: let's try to live without copy JSON.parse(JSON.stringify(va));
40+
}
41+
ret[key]=nval;
42+
}else{
43+
ret[key]=val;
44+
}
45+
};
46+
return ret;
47+
}
48+
mongoose.Schema.prototype.add = function add (obj, prefix) {
49+
ma.call(this,addI18n(obj),prefix);
50+
};
51+
})();
52+
53+
var localize=module.exports = function(opt){
54+
return {
55+
56+
}
57+
}
58+
localize.locales=function(){
59+
return locales;
60+
}

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"author": {
3+
"name": "Sergey Dolin"
4+
},
5+
"name": "mongoose-localize",
6+
"description": "A module to conver mongoose model property to set of localized properties.",
7+
"version": "0.0.2",
8+
"repository": {
9+
"url": ""
10+
},
11+
"main": "./index",
12+
"dependencies": {
13+
"mongoose": ">=3.8.8"
14+
},
15+
"engines": {
16+
"node": "*"
17+
},
18+
"scripts": {
19+
"test": "mocha test"
20+
}
21+
}

test/model.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var should = require('should'),
7+
mongoose = require('mongoose'),
8+
localize = require('mongoose-localize');
9+
// Application = mongoose.model('Application');
10+
11+
var TestSchema = new mongoose.Schema({
12+
name: {
13+
type: String,
14+
default: 'test',
15+
trim: true,
16+
localize: false
17+
}
18+
});
19+
mongoose.model('Test', TestSchema);
20+
var Test=mongoose.model('Test');
21+
22+
var TestLSchema = new mongoose.Schema({
23+
name: {
24+
type: String,
25+
default: 'testl',
26+
trim: true,
27+
localize: true
28+
}
29+
});
30+
mongoose.model('TestL', TestLSchema);
31+
var TestL=mongoose.model('TestL');
32+
33+
//The tests
34+
describe('<Mongoose Localize Test>', function() {
35+
36+
describe('Test Schema Available', function() {
37+
var t=new Test();
38+
it('non localized should have name="test" by default', function(done) {
39+
t.should.have.property('name','test');
40+
done();
41+
});
42+
});
43+
describe('Localized Test Schema Available', function() {
44+
var tl=new TestL();
45+
console.log(tl);
46+
it('localized should have property name', function(done) {
47+
tl.should.have.property('name');
48+
done();
49+
});
50+
});
51+
/*
52+
describe('Method Save', function() {
53+
it('should begin without the test user', function(done) {
54+
Application.find({ name: 'Some name' }, function(err, applications) {
55+
applications.should.have.length(0);
56+
done();
57+
});
58+
});
59+
60+
it('should be able to save without problems', function(done) {
61+
application.save(done);
62+
});
63+
});
64+
*/
65+
66+
});

0 commit comments

Comments
 (0)