Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Bug 929909 - Rename Context::get to getSync. r=gandalf
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Nov 4, 2013
1 parent 59039cd commit ebeac2c
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 244 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -64,7 +64,8 @@ ctx.requestLocales();
When you freeze the context by calling `requestLocales`, the resource files
will be retrieved, parsed and compiled. You can listen to the `ready` event
(emitted by the `Context` instance when all the resources have been compiled)
and use `ctx.get` and `ctx.getEntity` to get translations synchronously.
and use `ctx.getSync` and `ctx.getEntitySync` to get translations
synchronously.

Alternatively, you can register callbacks to execute when the context is ready
(or when globals change and translations need to be updated) with
Expand Down
9 changes: 5 additions & 4 deletions docs/api.md
Expand Up @@ -10,7 +10,8 @@ ctx.requestLocales();
When you freeze the context by calling `requestLocales`, the resource files
will be retrieved, parsed and compiled. You can listen to the `ready` event
(emitted by the `Context` instance when all the resources have been compiled)
and use `ctx.get` and `ctx.getEntity` to get translations synchronously.
and use `ctx.getSync` and `ctx.getEntitySync` to get translations
synchronously.

Alternatively, you can register callbacks to execute when the context is ready
(or when globals change and translations need to be updated) with
Expand Down Expand Up @@ -258,7 +259,7 @@ will make the internally-stored context data look like this:
```


### ctx.get(id: String, ctxdata: Object?)
### ctx.getSync(id: String, ctxdata: Object?)

Retrieve a string value of an entity called `id`.

Expand All @@ -268,7 +269,7 @@ extend the context data available for the evaluation of this entity.
Returns a string.


### ctx.getEntity(id: String, ctxdata: Object?)
### ctx.getEntitySync(id: String, ctxdata: Object?)

Retrieve an object with data evaluated from an entity called `id`.

Expand Down Expand Up @@ -307,7 +308,7 @@ translation of any of the entities changes.
The callback function is passed an `l10n` object with the following properties:

- `entities`: an object whose keys are the identifiers and value are the
entity objects as returned by `getEntity`,
entity objects as returned by `getEntitySync`,
- `reason`: an object with the reason why `callback` was invoked.

The `reason` object can be:
Expand Down
10 changes: 5 additions & 5 deletions lib/l20n/context.js
Expand Up @@ -209,8 +209,8 @@ define(function (require, exports) {
this.linkResource = linkResource;
this.updateData = updateData;

this.get = get;
this.getEntity = getEntity;
this.getSync = getSync;
this.getEntitySync = getEntitySync;
this.localize = localize;
this.ready = ready;

Expand Down Expand Up @@ -279,14 +279,14 @@ define(function (require, exports) {
extend(_data, obj);
}

function get(id, data) {
function getSync(id, data) {
if (!_isReady) {
throw new ContextError('Context not ready');
}
return getFromLocale.call(self, 0, id, data).value;
}

function getEntity(id, data) {
function getEntitySync(id, data) {
if (!_isReady) {
throw new ContextError('Context not ready');
}
Expand Down Expand Up @@ -380,7 +380,7 @@ define(function (require, exports) {
globalsUsed: {}
};
for (var i = 0, id; id = ids[i]; i++) {
many.entities[id] = getEntity.call(this, id);
many.entities[id] = getEntitySync.call(this, id);
for (var global in many.entities[id].globals) {
if (many.entities[id].globals.hasOwnProperty(global)) {
many.globalsUsed[global] = true;
Expand Down
28 changes: 14 additions & 14 deletions tests/integration/basic.js
Expand Up @@ -25,32 +25,32 @@ describe('A single-locale context with addResource', function() {
});

it('should return the string value of brandName', function() {
var value = ctx.get('brandName');
var value = ctx.getSync('brandName');
value.should.equal('Firefox');
});
it('should return the id of foo', function() {
var value = ctx.get('foo');
var value = ctx.getSync('foo');
value.should.equal('foo');
});
it('should return the value of about with the value of brandName in it', function() {
var value = ctx.get('about');
var value = ctx.getSync('about');
value.should.equal('About Firefox');
});
it('should return the value of cert with the value of $organization passed directly', function() {
var value = ctx.get('cert', {'organization': 'Mozilla Foundation'});
var value = ctx.getSync('cert', {'organization': 'Mozilla Foundation'});
value.should.equal('Certificate signed by Mozilla Foundation');
});
it('should return the value of cert with the value of $organization defined globally', function() {
ctx.updateData({ organization: 'Mozilla Foundation' });
var value = ctx.get('cert');
var value = ctx.getSync('cert');
value.should.equal('Certificate signed by Mozilla Foundation');
});
it('should return the id of brandNoDefault', function() {
var value = ctx.get('brandNoDefault');
var value = ctx.getSync('brandNoDefault');
value.should.equal('brandNoDefault');
});
it('should return the short value of brandWithDefault', function() {
var value = ctx.get('brandWithDefault');
var value = ctx.getSync('brandWithDefault');
value.should.equal('Firefox');
});

Expand All @@ -67,32 +67,32 @@ describe('A single-locale context with linkResource', function() {
});

it('should return the string value of brandName', function() {
var value = ctx.get('brandName');
var value = ctx.getSync('brandName');
value.should.equal('Firefox');
});
it('should return the id of foo', function() {
var value = ctx.get('foo');
var value = ctx.getSync('foo');
value.should.equal('foo');
});
it('should return the value of about with the value of brandName in it', function() {
var value = ctx.get('about');
var value = ctx.getSync('about');
value.should.equal('About Firefox');
});
it('should return the value of cert with the value of $organization passed directly', function() {
var value = ctx.get('cert', {'organization': 'Mozilla Foundation'});
var value = ctx.getSync('cert', {'organization': 'Mozilla Foundation'});
value.should.equal('Certificate signed by Mozilla Foundation');
});
it('should return the value of cert with the value of $organization defined globally', function() {
ctx.updateData({ organization: 'Mozilla Foundation' });
var value = ctx.get('cert');
var value = ctx.getSync('cert');
value.should.equal('Certificate signed by Mozilla Foundation');
});
it('should return the id of brandNoDefault', function() {
var value = ctx.get('brandNoDefault');
var value = ctx.getSync('brandNoDefault');
value.should.equal('brandNoDefault');
});
it('should return the short value of brandWithDefault', function() {
var value = ctx.get('brandWithDefault');
var value = ctx.getSync('brandWithDefault');
value.should.equal('Firefox');
});

Expand Down
116 changes: 58 additions & 58 deletions tests/lib/context/ctxdata.js
Expand Up @@ -33,11 +33,11 @@ describe('ctx.updateData', function() {
});

it('when absent, all testing entities don\'t work', function() {
ctx.get('foo').should.equal('{{ $foo }}');
ctx.get('bar').should.equal('{{ $bar }}');
ctx.get('barBar').should.equal('{{ $bar.bar }}');
ctx.get('barBaz').should.equal('{{ $bar.baz }}');
ctx.get('barBazQux').should.equal('{{ $bar.baz.qux }}');
ctx.getSync('foo').should.equal('{{ $foo }}');
ctx.getSync('bar').should.equal('{{ $bar }}');
ctx.getSync('barBar').should.equal('{{ $bar.bar }}');
ctx.getSync('barBaz').should.equal('{{ $bar.baz }}');
ctx.getSync('barBazQux').should.equal('{{ $bar.baz.qux }}');
});
it('should throw if the argument is not an object', function() {
(function() {
Expand All @@ -53,56 +53,56 @@ describe('ctx.updateData', function() {
ctx.updateData({
foo: 'F'
});
ctx.get('foo').should.equal('F');
ctx.getSync('foo').should.equal('F');
});
it('update foo, set bar', function() {
ctx.updateData({
foo: 'Foo',
bar: 'B'
});
ctx.get('foo').should.equal('Foo');
ctx.get('bar').should.equal('B');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('bar').should.equal('B');
});
it('update bar', function() {
ctx.updateData({
bar: 'Bar'
});
ctx.get('foo').should.equal('Foo');
ctx.get('bar').should.equal('Bar');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('bar').should.equal('Bar');
});
it('update bar to a dict', function() {
ctx.updateData({
bar: {
bar: 'BarBar'
}
});
ctx.get('bar').should.equal('{{ $bar }}');
ctx.get('barBar').should.equal('BarBar');
ctx.getSync('bar').should.equal('{{ $bar }}');
ctx.getSync('barBar').should.equal('BarBar');
});
it('add a member to bar', function() {
ctx.updateData({
bar: {
baz: 'BarBaz'
}
});
ctx.get('barBar').should.equal('BarBar');
ctx.get('barBaz').should.equal('BarBaz');
ctx.getSync('barBar').should.equal('BarBar');
ctx.getSync('barBaz').should.equal('BarBaz');
});
it('remove bar.baz', function() {
ctx.updateData({
bar: {
baz: undefined
}
});
ctx.get('barBar').should.equal('BarBar');
ctx.get('barBaz').should.equal('{{ $bar.baz }}');
ctx.getSync('barBar').should.equal('BarBar');
ctx.getSync('barBaz').should.equal('{{ $bar.baz }}');
});
it('update bar to a string', function() {
ctx.updateData({
bar: 'Bar'
});
ctx.get('foo').should.equal('Foo');
ctx.get('bar').should.equal('Bar');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('bar').should.equal('Bar');
});
it('update bar to a dict again', function() {
ctx.updateData({
Expand All @@ -111,8 +111,8 @@ describe('ctx.updateData', function() {
baz: 'BarBaz'
}
});
ctx.get('barBar').should.equal('BarBar');
ctx.get('barBaz').should.equal('BarBaz');
ctx.getSync('barBar').should.equal('BarBar');
ctx.getSync('barBaz').should.equal('BarBaz');
});
it('update bar.baz to a dict', function() {
ctx.updateData({
Expand All @@ -122,9 +122,9 @@ describe('ctx.updateData', function() {
}
}
});
ctx.get('barBar').should.equal('BarBar');
ctx.get('barBaz').should.equal('{{ $bar.baz }}');
ctx.get('barBazQux').should.equal('BarBazQux');
ctx.getSync('barBar').should.equal('BarBar');
ctx.getSync('barBaz').should.equal('{{ $bar.baz }}');
ctx.getSync('barBazQux').should.equal('BarBazQux');
});
it('remove bar.baz.qux', function() {
ctx.updateData({
Expand All @@ -134,23 +134,23 @@ describe('ctx.updateData', function() {
}
}
});
ctx.get('barBar').should.equal('BarBar');
ctx.get('barBaz').should.equal('{{ $bar.baz }}');
ctx.get('barBazQux').should.equal('{{ $bar.baz.qux }}');
ctx.getSync('barBar').should.equal('BarBar');
ctx.getSync('barBaz').should.equal('{{ $bar.baz }}');
ctx.getSync('barBazQux').should.equal('{{ $bar.baz.qux }}');
});
it('remove bar', function() {
ctx.updateData({
bar: undefined
});
ctx.get('foo').should.equal('Foo');
ctx.get('bar').should.equal('{{ $bar }}');
ctx.get('barBar').should.equal('{{ $bar.bar }}');
ctx.get('barBaz').should.equal('{{ $bar.baz }}');
ctx.get('barBazQux').should.equal('{{ $bar.baz.qux }}');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('bar').should.equal('{{ $bar }}');
ctx.getSync('barBar').should.equal('{{ $bar.bar }}');
ctx.getSync('barBaz').should.equal('{{ $bar.baz }}');
ctx.getSync('barBazQux').should.equal('{{ $bar.baz.qux }}');
});
});

describe('ctx.get with ctxdata passed directly', function() {
describe('ctx.getSync with ctxdata passed directly', function() {
'use strict';
var ctx = new Context();
ctx.updateData({
Expand All @@ -173,42 +173,42 @@ describe('ctx.get with ctxdata passed directly', function() {
});

it('does nothing if no data is paased', function() {
ctx.get('foo').should.equal('Foo');
ctx.get('userName').should.equal('Bob');
ctx.get('userGender').should.equal('masculine');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('userName').should.equal('Bob');
ctx.getSync('userGender').should.equal('masculine');
});
it('overrides the global primitive value', function() {
ctx.get('foo', { foo: 'Bar' }).should.equal('Bar');
ctx.get('foo').should.equal('Foo');
ctx.get('userName').should.equal('Bob');
ctx.get('userGender').should.equal('masculine');
ctx.getSync('foo', { foo: 'Bar' }).should.equal('Bar');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('userName').should.equal('Bob');
ctx.getSync('userGender').should.equal('masculine');
});
it('adds a new object', function() {
ctx.get('fooBar').should.equal('{{ $foo.bar }}');
ctx.get('foo', { foo: { bar: 'Bar' } }).should.equal('{{ $foo }}');
ctx.get('fooBar', { foo: { bar: 'FooBar' } }).should.equal('FooBar');
ctx.get('foo').should.equal('Foo');
ctx.getSync('fooBar').should.equal('{{ $foo.bar }}');
ctx.getSync('foo', { foo: { bar: 'Bar' } }).should.equal('{{ $foo }}');
ctx.getSync('fooBar', { foo: { bar: 'FooBar' } }).should.equal('FooBar');
ctx.getSync('foo').should.equal('Foo');
});
it('overrides the global second-level primitive value', function() {
ctx.get('userName', { user: { name: 'Ben' } }).should.equal('Ben');
ctx.get('userName').should.equal('Bob');
ctx.getSync('userName', { user: { name: 'Ben' } }).should.equal('Ben');
ctx.getSync('userName').should.equal('Bob');
});
it('does not change other second-level members', function() {
ctx.get('userName', { user: { name: 'Ben' } }).should.equal('Ben');
ctx.get('userGender', { user: { name: 'Ben' } }).should.equal('masculine');
ctx.getSync('userName', { user: { name: 'Ben' } }).should.equal('Ben');
ctx.getSync('userGender', { user: { name: 'Ben' } }).should.equal('masculine');
});
it('removes second-level members, but only locally', function() {
ctx.get('userName', {
ctx.getSync('userName', {
user: {
name: undefined
}
}).should.equal('{{ $user.name }}');
ctx.get('userName').should.equal('Bob');
ctx.getSync('userName').should.equal('Bob');
});
it('removes first-level members, but only locally', function() {
ctx.get('userName', { user: undefined }).should.equal('{{ $user.name }}');
ctx.get('userName').should.equal('Bob');
ctx.get('userGender').should.equal('masculine');
ctx.getSync('userName', { user: undefined }).should.equal('{{ $user.name }}');
ctx.getSync('userName').should.equal('Bob');
ctx.getSync('userGender').should.equal('masculine');
});
});

Expand All @@ -230,16 +230,16 @@ describe('Object.prototype manipulation', function() {
});

it('neither $foo nor $bar are defined', function() {
ctx.get('foo').should.equal('{{ $foo }}');
ctx.get('bar').should.equal('{{ $bar }}');
ctx.getSync('foo').should.equal('{{ $foo }}');
ctx.getSync('bar').should.equal('{{ $bar }}');
});
it('after updateData(), only $foo is defined', function() {
ctx.updateData({ foo: 'Foo' });
ctx.get('foo').should.equal('Foo');
ctx.get('bar').should.equal('{{ $bar }}');
ctx.getSync('foo').should.equal('Foo');
ctx.getSync('bar').should.equal('{{ $bar }}');
});
it('when passed directly, only $foo is defined', function() {
ctx.get('foo', { foo: 'Foo' }).should.equal('Foo');
ctx.get('bar', { foo: 'Foo' }).should.equal('{{ $bar }}');
ctx.getSync('foo', { foo: 'Foo' }).should.equal('Foo');
ctx.getSync('bar', { foo: 'Foo' }).should.equal('{{ $bar }}');
});
});

0 comments on commit ebeac2c

Please sign in to comment.