Created
December 22, 2010 19:52
-
-
Save dvv/752005 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# how object capabilities can be made by means of kriszyp's Compose | |
# | |
# BIG FAT WARNING #1 | |
# Compose internal mixin function treats objects with .get/.set/.value as ES5 property definition | |
# Opened the issue on that. | |
# BIG FAT WARNING #2 | |
# methods which are not aspect()ed, can't so far be Compose'd.from() | |
db = new Storage dbParams # external implementation | |
Store = (entity) -> | |
find: db.find.bind db, entity | |
get: db.get.bind db, entity | |
save: db.save.bind db, entity | |
add: db.insert.bind db, entity | |
remove: db.remove.bind db, entity | |
update: db.update.bind db, entity | |
patch: db.patch.bind db, entity | |
drop: db.drop.bind db, entity | |
Model = (entity, overrides) -> | |
Compose.create Store(entity), overrides | |
Facet = (model, expose) -> | |
facet = {} | |
expose.forEach (name) -> | |
facet[name] = Compose.from(model, name).bind model | |
Compose.create {}, facet | |
# ... | |
model = {} | |
model.Foo = Model 'Foo', | |
update: Compose.around (base) -> | |
(docIn) -> | |
console.log 'BEFOREUPDATE', arguments | |
# don't let some props in | |
# say, kick off 'password', and access.write | |
# FIXME: RQL's unselect() should be used | |
Object.veto docIn, ['password', ['access', 'write']] | |
_when base.call(@, docIn), (docOut) -> | |
# add some props | |
docOut._version = 2 | |
# don't let some props out, as security measure | |
# say, kick off 'id' prop, and prop bar of prop foo | |
Object.veto docOut, ['id', ['foo', 'bar']] | |
console.log 'AFTERUPDATE', docOut | |
docOut | |
model.Bar = Model 'Bar', | |
# calling inherited method w/o help of Compose.around() | |
# N.B. such methods can't so far be Compose'd.from() | |
find: (query) -> @__proto__.find Query(query).ne('restriction', null) | |
foos: () -> @find "foo!=null" | |
facets = {} | |
facets.admin = | |
Bar: Facet model.Bar, ['foos'] | |
# ...................... | |
# Request handler | |
Step {available: 'as "this" inside steps', req: req, res: res}, [ | |
() -> | |
# get the session | |
sid = @req.getSecureCookie 'sid' | |
model.Session.get sid | |
(session) -> | |
session ?= {} | |
# get the user | |
uid = session.uid | |
model.User.get uid | |
(user) -> | |
user ?= {} | |
# get the user capabilities | |
facets[getUserLevel user] | |
(context) -> | |
context ?= {} | |
# given URL and HTTP method, dispatch the request | |
# N.B. if context.Bar, or context.Bar.foos is not set -> | |
# we'll just get TypeError exception which is processed by Response#send as 403 | |
if url is 'Bar/foos' and method is 'GET' | |
return context.Bar.foos location.search | |
... | |
null # means 404 | |
(response) -> | |
@res.send response | |
(err) -> | |
# exception in course of @res.send()... | |
console.log 'Should not have been here!', err | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment