Skip to content

Instantly share code, notes, and snippets.

@elclanrs
Created November 18, 2013 10:39
Show Gist options
  • Save elclanrs/7525809 to your computer and use it in GitHub Desktop.
Save elclanrs/7525809 to your computer and use it in GitHub Desktop.
Building.js
{each, filter} = require \prelude-ls
@Building =
overload: (fns) -> (...args) ->
types = args.map -> typeof! it
i = 0
while true
fn = fns[types * ', ']
break if fn or i > args.length
types.pop!
i++
fn = fn or fns.any or ->
if types.length isnt fn.length
return fn.apply @, args[0, fn.length-1] ++ args[fn.length-1]
fn ...
new: (app=\App) ->
config = {}
modules = {}
methods = {}
hooks = []
{app} <<<
config: @overload do
any: -> config
'String': (prop) ->
config[prop]
'Object': (props) ->
config <<< props
@
'String, Object': (key, props) ->
config[key] = props
@
module: @overload do ->
'String': (module) ->
modules[module].apply @, &[1 to]
'String, Function': (module, fn) ->
modules[module] = (scope) ~>
returned = fn ...
return returned if returned?
scope
@
hook: (hook, ...args) ->
hooks
|> filter ~> it.hook is hook and it.component in @components
|> each ~> it.fn.apply @, args
component: (component, hooks-fn) ->
@[]components.push component
for hook, fn of hooks-fn ...
hooks.push {component, hook, fn}
@
method: (method, fn) ->
methods[method] = fn
@
bootstrap: (boot=->) ->
class App implements @
@method = (method, fn) ->
@::[method] = ->
fn ...
@
for method, fn of methods
@method method, fn
(@settings={}) ~>
boot ...
@init?!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment