diff --git a/README.md b/README.md index d273025..89584d5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ YCB is a multi-dimensional configuration library that builds bundles from resour More info on the [wiki](https://github.com/yahoo/ycb/wiki). +Examples are provided in the [this directory](https://github.com/yahoo/ycb/tree/master/tests). + ### Install `npm install ycb --save` diff --git a/examples/full/README.md b/examples/full/README.md new file mode 100644 index 0000000..c3b9e4d --- /dev/null +++ b/examples/full/README.md @@ -0,0 +1,16 @@ + + +full +====== + +This examples shows how to use a more realistic YCB dimensions and +configuration to "contextualized" a set of configuration. + +To run this example: + + cd ../../ && npm i && cd examples/full + node app.js + + + + diff --git a/examples/full/app.js b/examples/full/app.js new file mode 100644 index 0000000..83e755c --- /dev/null +++ b/examples/full/app.js @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved. + * Copyrights licensed under the New BSD License. + * See the accompanying LICENSE file for terms. + */ + +/*jslint node:true, nomen:true*/ + +var libfs = require('fs'), + libpath = require('path'), + libycb = require('../../'), + assert = require('assert'), + data, + ycb, + context, + config; + +/** +@param {string} file JSON valid file representing a configuration +@return {object} the parsed version of `file` +**/ +function readFile(file) { + var raw = libfs.readFileSync(libpath.join(__dirname, file)); + return JSON.parse(raw); +} + +data = readFile('dimensions.json').concat(readFile('application.json')); + +// create a new Ycb instance with `data` +ycb = new libycb.Ycb(data, {}); + +// read "master" +config = ycb.read({}); +assert.equal(8000, config.appPort); +assert.equal(false, config.viewEngine.cacheTemplates); +assert.equal(undefined, config.selector); + + +// read "device:opera-mini" +config = ycb.read({ device: 'opera-mini' }); +assert.equal('opera-mini', config.selector); +assert.equal(8000, config.appPort); + +// read "environment:development" +config = ycb.read({ environment: 'development' }); +assert.equal(false, config.yui.config.combine); +assert.equal("debug", config.yui.config.logLevel); +assert.equal(false, config.yui.config.fetchCSS); + +config = ycb.readNoMerge({ environment: 'development' }, {}); +assert.equal(2, config.length); diff --git a/examples/full/application.json b/examples/full/application.json new file mode 100644 index 0000000..9b48e38 --- /dev/null +++ b/examples/full/application.json @@ -0,0 +1,90 @@ +[{ + "settings": ["master"], + + "appPort": 8000, + "builds": { + "html5app": { + "attachManifest": true, + "forceRelativePaths": true, + "urls": ["/read.html"] + } + }, + "viewEngine": { + "cacheTemplates": false + }, + "yui": { + "config": { + "combine": true, + "logLevel": "info", + "fetchCSS": false + } + }, + + "specs": { + + "_frame": { + "type": "HTMLFrameMojit", + "config": { + "deploy": true, + "assets": { + "top": { + "blob": [""], + "css": ["/static/Frame/assets/reset.css"] + } + } + } + }, + + "shelf": { + "base": "_frame", + "config": { + "title": "NewsBoxes", + "child": { + "type": "Shelf", + "config": { + "children": { + "weather": {"type": "Weather"} + }, + "assets": { + "top": { + "css": ["/static/Shelf/assets/shelf.css"] + } + } + } + } + } + }, + + "read": { + "base": "_frame", + "config": { + "title": "NewsBoxes ยป Read", + "child": { + "type": "Read", + "config": { + "assets": { + "top": {"css": ["/static/Read/assets/read.css"]} + } + } + } + } + } + } +}, +{ + "settings": [ "device:opera-mini" ], + "selector": "opera-mini" +}, +{ + "settings": [ "environment:development" ], + "staticHandling": { + "forceUpdate": true, + "serveYUIFromAppOrigin": true + }, + "yui": { + "config": { + "combine": false, + "logLevel": "debug" + } + } +}] diff --git a/examples/full/dimensions.json b/examples/full/dimensions.json new file mode 100644 index 0000000..32778a8 --- /dev/null +++ b/examples/full/dimensions.json @@ -0,0 +1,183 @@ +[ + { + "dimensions": [ + { + "runtime": { + "common": { + "client": null, + "server": null + } + } + }, + { + "device": { + "android": null, + "blackberry": null, + "iemobile": null, + "iphone": null, + "ipad": null, + "kindle": null, + "opera-mini": null, + "palm": null + } + }, + { + "environment": { + "development": { + "dev": null, + "test": null + }, + "production": { + "stage": null, + "prod": null + } + } + }, + { + "lang": { + "ar": { + "ar-JO": null, + "ar-MA": null, + "ar-SA": null, + "ar-EG": null + }, + "bn": { + "bn-IN": null + }, + "ca": { + "ca-ES": null + }, + "cs": { + "cs-CZ": null + }, + "da": { + "da-DK": null + }, + "de": { + "de-AT": null, + "de-DE": null + }, + "el": { + "el-GR": null + }, + "en": { + "en-AU": null, + "en-BG": null, + "en-CA": null, + "en-GB": null, + "en-GY": null, + "en-HK": null, + "en-IE": null, + "en-IN": null, + "en-MY": null, + "en-NZ": null, + "en-PH": null, + "en-SG": null, + "en-US": null, + "en-ZA": null + }, + "es": { + "es-AR": null, + "es-BO": null, + "es-CL": null, + "es-CO": null, + "es-EC": null, + "es-ES": null, + "es-MX": null, + "es-PE": null, + "es-PY": null, + "es-US": null, + "es-UY": null, + "es-VE": null + }, + "fi": { + "fi-FI": null + }, + "fr": { + "fr-BE": null, + "fr-CA": null, + "fr-FR": null, + "fr-GF": null + }, + "hi": { + "hi-IN": null + }, + "hu": { + "hu-HU": null + }, + "id": { + "id-ID": null + }, + "it": { + "it-IT": null + }, + "ja": { + "ja-JP": null + }, + "kn": { + "kn-IN": null + }, + "ko": { + "ko-KR": null + }, + "ml": { + "ml-IN": null + }, + "mr": { + "mr-IN": null + }, + "ms": { + "ms-MY": null + }, + "nb": { + "nb-NO": null + }, + "nl": { + "nl-BE": null, + "nl-NL": null, + "nl-SR": null + }, + "pl": { + "pl-PL": null + }, + "pt": { + "pt-BR": null + }, + "ro": { + "ro-RO": null + }, + "ru": { + "ru-RU": null + }, + "sv": { + "sv-SE": null + }, + "ta": { + "ta-IN": null + }, + "te": { + "te-IN": null + }, + "th": { + "th-TH": null + }, + "tr": { + "tr-TR": null + }, + "vi": { + "vi-VN": null + }, + "zh": { + "zh-Hans": { + "zh-Hans-CN": null + }, + "zh-Hant": { + "zh-Hant-HK": null, + "zh-Hant-TW": null + } + } + } + } + ] + } +] diff --git a/examples/full/package.json b/examples/full/package.json new file mode 100644 index 0000000..7c9fada --- /dev/null +++ b/examples/full/package.json @@ -0,0 +1,11 @@ +{ + "name": "full", + "description": "A more complete example of how to use ycb.", + "author": "nobody", + "version": "0.1.0", + "keywords": [ + "ycb" + ], + "dependencies": { + } +} diff --git a/examples/simple/README.md b/examples/simple/README.md new file mode 100644 index 0000000..f33dc8d --- /dev/null +++ b/examples/simple/README.md @@ -0,0 +1,16 @@ + + +simple +====== + +This examples shows how to use a simple YCB dimensions and configuration to +"contextualized" a set of configuration. + +To run this example: + + cd ../../ && npm i && cd examples/simple + node app.js + + + + diff --git a/examples/simple/app.js b/examples/simple/app.js new file mode 100644 index 0000000..fffcd38 --- /dev/null +++ b/examples/simple/app.js @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved. + * Copyrights licensed under the New BSD License. + * See the accompanying LICENSE file for terms. + */ + +/*jslint node:true, nomen:true*/ + +var libfs = require('fs'), + libpath = require('path'), + libycb = require('../../'), + assert = require('assert'), + data, + ycb, + context, + config; + +/** +@param {string} file JSON valid file representing a configuration +@return {object} the parsed version of `file` +**/ +function readFile(file) { + var raw = libfs.readFileSync(libpath.join(__dirname, file)); + return JSON.parse(raw); +} + +data = readFile('dimensions.json').concat(readFile('application.json')); + +// create a new Ycb instance with `data` +ycb = new libycb.Ycb(data, {}); + +// read "master" +config = ycb.read({}); +assert.equal(8666, config.appPort); + +// read "environment:prod" +config = ycb.read({ environment: 'prod' }); +assert.equal(80, config.appPort); + +// read "device:desktop" +config = ycb.read({ device: 'desktop' }); +assert.equal(8080, config.appPort); + +// read "environment:prod", "device:desktop" +config = ycb.read({ environment: 'prod', device: 'smartphone' }); +assert.equal(8888, config.appPort); + diff --git a/examples/simple/application.json b/examples/simple/application.json new file mode 100644 index 0000000..c186a26 --- /dev/null +++ b/examples/simple/application.json @@ -0,0 +1,18 @@ +[ + { + "settings": [ "master" ], + "appPort": 8666 + }, + { + "settings": [ "environment:prod" ], + "appPort": 80 + }, + { + "settings": [ "device:desktop" ], + "appPort": 8080 + }, + { + "settings": [ "environment:prod", "device:smartphone" ], + "appPort": 8888 + } +] diff --git a/examples/simple/dimensions.json b/examples/simple/dimensions.json new file mode 100644 index 0000000..b343a1c --- /dev/null +++ b/examples/simple/dimensions.json @@ -0,0 +1,21 @@ +[ + { + "dimensions": [ + { + "environment": { + "testing": null, + "prod": null + } + }, + { + "device": { + "desktop": null, + "mobile": { + "table": null, + "smartphone": null + } + } + } + ] + } +] diff --git a/examples/simple/package.json b/examples/simple/package.json new file mode 100644 index 0000000..8c7a386 --- /dev/null +++ b/examples/simple/package.json @@ -0,0 +1,10 @@ +{ + "name": "simple", + "description": "A simple example of how to use ycb.", + "version": "0.1.0", + "keywords": [ + "ycb" + ], + "dependencies": { + } +}