Skip to content

Instantly share code, notes, and snippets.

@zorbash
Created April 7, 2015 20:23
Show Gist options
  • Select an option

  • Save zorbash/3adefff22a2d68e2414b to your computer and use it in GitHub Desktop.

Select an option

Save zorbash/3adefff22a2d68e2414b to your computer and use it in GitHub Desktop.
Basic module
define ->
moduleKeywords = ['extended', 'included']
###
Ruby-like mixin functionality for CoffeeScript
@see http://arcturo.github.io/library/coffeescript/03_classes.html
###
class Module
###
Copies the properties of the mixin onto the object
@param obj [Object] A mixin
```
# To extend a Class
@extend SomeClass::
# To extend an Object
@extend SomeObject
# Keep in mind that :: in coffee stands for prototype
```
###
@extend: (obj) ->
for key, value of obj when key not in moduleKeywords
@[key] = value
obj.extended?.apply(@)
@
###
Copies the mixins properties onto the prototype
The mixin is either a Class or an Object
@note To include classes do the following
```
# To include a Class
@include SomeClass::
# To include an Object
@include SomeObject
```
@param obj [Object] A mixin
###
@include: (obj) ->
for key, value of obj when key not in moduleKeywords
# Assign properties to the prototype
@::[key] = value
obj.included?.apply(@)
@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment