Created
July 17, 2015 05:21
-
-
Save wycleffsean/6ac8ff080e3833e5599f to your computer and use it in GitHub Desktop.
Coffeescript Mixins
This file contains 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
Mixin = require './mixin.coffee' | |
Module = require './module.coffee' | |
class Logger extends Mixin | |
yo = (msg) -> console.log msg | |
class_log: yo | |
@log: yo | |
class MyLogger extends Module | |
@extends Logger | |
class MyOtherLogger extends Module | |
@extends Logger | |
test = new MyLogger | |
test.log 'yo' | |
MyAlerter.class_log 'class yo' | |
x.class_alert 'neat-o' for x in Alerter::extensions |
This file contains 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
class Mixin | |
extensions: [] | |
extended: (obj) -> @extensions.push obj | |
module.exports = Mixin |
This file contains 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
MIXIN_KEYWORDS = ['extended', 'extensions'] | |
IGNORED_KEYS = ['constructor', '__super__'].concat MIXIN_KEYWORDS | |
class Module | |
# Private Methods | |
materialize = (obj) -> | |
if obj instanceof Function then new obj() else obj | |
extend = (target, obj) -> | |
for key, value of obj when key not in IGNORED_KEYS | |
target::[key] = value | |
for key, value of (obj::) when key not in IGNORED_KEYS | |
target[key] = value | |
# Class Methods | |
@extends: (obj) -> | |
extend @, obj | |
materialize(obj).extended?(@) | |
this | |
module.exports = Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment