Skip to content

Commit

Permalink
Last cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
winton committed May 24, 2015
1 parent d514626 commit 8c9fa15
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions lib/mixin.coffee
@@ -1,11 +1,15 @@
module.exports = (klasses..., options={}) ->
unless typeof options == "object"
klasses.push(options)
options = {}

klasses.reduce (current, from, index, array) ->
from.__super__ ||= current::

wrapFunctions current::, from::, options
wrapFunctions merge
to: current::
from: from::
options

class
@include from
Expand All @@ -15,9 +19,12 @@ module.exports = (klasses..., options={}) ->
@extend current

constructor: ->
return wrapFunction(
from, arguments, current::.constructor, @, options
)
return wrapFunction merge
bind: @
fn: from
fnSuper: current::.constructor
args: arguments
options

makeArray = (a) ->
if !a || a instanceof Array then a else [ a ]
Expand All @@ -29,26 +36,35 @@ merge = (to, from) ->

to

wrapFunctions = (to, from, options={}) =>
wrapFunctions = (options={}) =>
{ to, from } = options

for name, fn of from
do (name, fn) ->
stop = name == "constructor"
stop ||= typeof from[name] != "function"

unless stop
from[name] = ->
wrapFunction fn, arguments, to[name], from, options
wrapFunction merge
bind: from
fn: fn
fnSuper: to[name]
args: arguments
options

wrapFunction = (options={}) ->
{ fn, args, fnSuper, bind, prepend, append } = options

wrapFunction = (fn, fnArgs, fnSuper, bind, options={}) ->
if options.prepend && fnSuper
args = fnSuper.apply bind, fnArgs
if prepend && fnSuper
result = fnSuper.apply bind, args

args = fn.apply bind, makeArray(args) || fnArgs
result = fn.apply bind, makeArray(result) || args

if options.append && fnSuper
args = fnSuper.apply bind, makeArray(args)
if append && fnSuper
result = fnSuper.apply bind, makeArray(result)

args
result

# Function extensions.
#
Expand Down

0 comments on commit 8c9fa15

Please sign in to comment.