Deprecated
This project is deprecated. Use easy-app instead.
the-box
Can you see what's wrong with this code?
var db =var user =var res =db
Actually it is a very neat piece of code except that there is no way to require
response
with node.js require system.
This project is about making the above snippet fully functional and it seems it
succeeded. It completely removes artificial boundary between static, boot-time,
request-time, whatever-time dependencies allowing you super easily define as
many layers as you want while completely freeing modules from dependency issues.
This project was inspired by The-Kiln
and is nothing but an implementation of
dependency based computational model :)
similar to what is used by build systems (like rake, make) and by node.js
require
.
How it looks like
var express =var App =var app =appappappappapp
Explanation
The app.def()
method defines what is internally called a box, aka task (in
rake), aka module (in node).
Once the box was defined we can evaluate it.
app
A value of evaluated box is cached, so subsequent evals do not result in repeated calls to the definition function.
Another way to define box is to use app.set()
method
app
We can get a value of evaluated box with app.get()
. It returns undefined
if
box doesn't exist or not yet evaluated.
Boxes themselves also can get values:
app
and framework ensures that before evaluation of the box all its dependencies were evaluated. There are two ways to specify dependencies
// list them explicitlyapp// or allow to infer them from the function sourceapp
Of course, boxes can be asynchronous
app
Perhaps the most strongest point of the-box
is how it manages dependency
levels. You can just create a new app instance with Object.create(app)
and
this instance will inherit all box definitions and every evaluated box will
remain evaluated while subsequent manipulations (evals, defs, etc) with new
instance will not change the parent app.
app
Misc details
All boxes are evaluated sequentially.
paths
There is a concept of path
app
app// is the same asapp
this
of the box is set to app.at('box/path')
app
Error handling
Errors from boxes (both sync and async) are catched and can be handled.
appapp
Errors are bubbling. So if the handler for foo/bar/baz
wouldn't be defined the
handler for foo/bar
would be checked and so on up to the root level handler
(app.onerror(fn)
) which by default just throws a given error. It is possible
to rethrow catched errors
appappappappappapp
Hooks
It is possible to define before
and after
hooks for any box. Before
hooks
are ordinal boxes which are executed before "main" box and it's dependencies.
After
hooks are also boxes (they can have dependencies, etc) but their signature
is slightly different.
appappapp
An async version of after hook has (get, val, done)
signature.
It is not important when to define hooks. They can be defined before or after corresponding boxes.
// Example: Automatically parse the request bodyvar bodyParser = expressapp{app}
Installation
Via npm
npm install the-box
To run tests first install dev dependencies and then run npm test command.
npm install -d
npm test
Misc
express-in-the-box project is an integration of awesome express request-response prototypes and router with the-box container.
License
(The MIT License)
Copyright (c) 2012 Eldar Gabdullin eldargab@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.