maritz/nohm — Issue #36: Introducing Nohm.relations
Author: yuchi
Created: 2011-12-12 22:23:25+00:00
State: open
Labels: Improvement

Something that dries to:

```javascript
Nohm.relations = {
'child' : 'parent'
};

Nohm.__reversedRelations = function () {
var reversed = {};
for (var child in Nohm.relations) {
if (Nohm.relations.hasOwnProperty(child)) {
reversed[Nohm.relations[child]] = child;
}
}
return reversed;
};

Nohm.getRelationParentName = function (name) {
var parent = Nohm.relations[name];
return parent || Nohm.toDefaultParentName(name);
};

Nohm.getRelationChildName = function (name) {
return Nohm.__reversedRelations()[name] || Nohm.toDefaultChildName(name);
};

Nohm.toDefaultParentName = function (name) {
return name + 'Parent';
};

Nohm.toDefaultChildName = function (name) {
// regexp? :(
};
```

This way I could be able to write:

```javascript
Nohm.relations[ 'grandson' ] = 'grandpa';
```

And I'm not forced to use `name` and `nameParent`, that sometimes is confusing.