Permalink
Browse files

Refactor essential helpers registration

  • Loading branch information...
1 parent 6a6d016 commit 472e68251fa95d1dbd61d0f80ea8b4fe5bd084c9 @ixti ixti committed Nov 21, 2012
Showing with 49 additions and 30 deletions.
  1. +1 −1 examples/assets/stylesheets/app.css.ejs
  2. +5 −5 examples/environment.js
  3. +43 −24 lib/mincer/context.js
@@ -3,7 +3,7 @@
**/
body {
- background: url('<%= asset_path('stripes.png') %>');
+ background: url('<%= image_path('stripes.png') %>');
}
h1 {
View
@@ -42,16 +42,16 @@ environment.appendPath('vendor/bootstrap/less');
//
-// Define environment helper that will be available in the processed assets
-// See `assets/stylesheets/app.css.ejs` for example of `asset_path` usage.
+// Define environment essential *_path helper that will be available in the
+// processed assets. See `assets/stylesheets/app.css.ejs` for example.
//
-environment.registerHelper('asset_path', function (logicalPath) {
- var asset = environment.findAsset(logicalPath);
+environment.ContextClass.defineAssetPath(function (pathname, options) {
+ var asset = this.environment.findAsset(pathname, options);
if (!asset) {
- throw new Error("File " + logicalPath + " not found");
+ throw new Error("File " + pathname + " not found");
}
return '/assets/' + asset.digestPath;
View
@@ -67,14 +67,10 @@ var Context = module.exports = function Context(environment, logicalPath, pathna
// Hash of helpers available as locals in the renderers.
-// For each helper, a wrpper will be generated to preserve `this` context.
+// For each helper, a wrapper will be generated to preserve `this` context.
//
// See Context.registerHelper()
-prop(Context.prototype, '__helpers__', {
- // note we'll need to backpatch `func` later
- asset_data_uri: {name: 'asset_data_uri', func: Context.prototype.assetDataUri},
- asset_path: {name: 'asset_path', func: Context.prototype.assetPath}
-});
+prop(Context.prototype, '__helpers__', {});
/**
@@ -88,10 +84,10 @@ prop(Context.prototype, '__helpers__', {
* By default this method is not implemented, and you must provide it's
* implementation that will fit your needs, e.g.:
*
- * environment.ContextClass.prototype.assetPath = function (pathname, options) {
+ * environment.ContextClass.defineAssetPath(function (pathname, options) {
* var asset = this.environment.findAsset(pathname, options);
* return '/assets/' + asset.digestPath;
- * };
+ * });
*
* Once implemented, you can use `asset_path` helper, e.g.:
*
@@ -101,12 +97,11 @@ Context.prototype.assetPath = function assetPath() {
throw new Error(
"Custom asset_path helper is not implemented\n\n" +
"Extend your environment context with a custom method.\n\n" +
- " environment.ContextClass.prototype.assetPath = function (pathname, options) {\n" +
+ " environment.ContextClass.defineAssetPath(function (pathname, options) {\n" +
" // ... your code here ... \n" +
- " };"
+ " });"
);
};
-Context.prototype.__helpers__['asset_path'].func = Context.prototype.assetPath;
/**
@@ -152,16 +147,9 @@ Context.prototype.__helpers__['asset_path'].func = Context.prototype.assetPath;
**/
['image', 'video', 'audio', 'font', 'javascript', 'stylesheet'].forEach(function (assetType) {
- var func = function (pathname) {
+ Context.prototype[assetType + 'Path'] = function (pathname) {
return this.assetPath(pathname, {type: assetType});
};
- //Context.registerHelper(assetType + '_path', func);
- var helper = {
- name: assetType + '_path',
- func: func
- };
- Context.prototype.__helpers__[assetType + '_path'] = helper;
- Context.prototype[assetType + 'Path'] = func;
});
@@ -176,6 +164,7 @@ Context.prototype.__helpers__['asset_path'].func = Context.prototype.assetPath;
**/
getter(Context.prototype, 'rootPath', function () {
var pathname = this.pathname;
+
return _.find(this.environment.paths, function (path) {
return pathname.substr(0, path.length) === path;
}).substr(this.environment.root.length + 1);
@@ -388,11 +377,6 @@ Context.prototype.assetDataUri = function (pathname) {
};
-Context.prototype.__helpers__['asset_data_uri'] = {
- func: Context.prototype.assetDataUri
-};
-
-
// returns new Error with anotated message telling where error happened.
// original error can be accessed via `originalError` property.
function beautifyError(err, pathname) {
@@ -514,6 +498,38 @@ function define_helpers_registrator(Klass) {
define_helpers_registrator(Context);
+// Register all standard (built-in) helpers
+Context.registerHelper({
+ 'asset_data_uri': Context.prototype.assetDataUri,
+ 'asset_path': Context.prototype.assetPath,
+ 'image_path': Context.prototype.imagePath,
+ 'video_path': Context.prototype.videoPath,
+ 'audio_path': Context.prototype.audioPath,
+ 'font_path': Context.prototype.fontPath,
+ 'javascript_path': Context.prototype.javascriptPath,
+ 'stylesheet_path': Context.prototype.stylesheetPath
+});
+
+
+/**
+ * Context.defineAssetPath(func) -> Void
+ *
+ * Syntax sugar that provides an easy way to set real implementation of
+ * `assetPath` and propose it to helpers.
+ *
+ * ##### Example:
+ *
+ * Context.defineAssetPath(function (pathname, options) {
+ * var asset = this.environment.findAsset(pathname, options);
+ * return '/assets/' + asset.digestPath;
+ * });
+ **/
+Context.defineAssetPath = function (func) {
+ this.prototype.assetPath = func;
+ this.registerHelper('asset_path', func);
+};
+
+
/** internal
* Context#subclass -> Function
*
@@ -525,6 +541,9 @@ getter(Context, 'subclass', function () {
// add class inheritance
inherits(Klass, Context);
+ // re-expose syntax sugar for custom asset_path helper setter
+ Klass.defineAssetPath = Context.defineAssetPath;
+
// clone some own-prototype properties
prop(Klass.prototype, '__helpers__', _.clone(Context.prototype.__helpers__));

0 comments on commit 472e682

Please sign in to comment.