Skip to content

Instantly share code, notes, and snippets.

@dsci
Forked from lancejpollard/class.coffee
Last active December 14, 2015 23:59
Show Gist options
  • Save dsci/5169780 to your computer and use it in GitHub Desktop.
Save dsci/5169780 to your computer and use it in GitHub Desktop.
moduleKeywords = ['included', 'extended', 'prototype']
class Class
# Rename an instance method
#
# ``` coffeescript
# class User
# @alias "methods", "instance_methods"
#
# ```
@alias: (to, from) ->
@::[to] = @::[from]
@alias_method: (to, from) ->
@::[to] = @::[from]
@delegate: ->
options = arguments.pop()
to = options.to
self = @
for key in arguments
self::[key] = to[key]
@include: (obj) ->
throw new Error('include(obj) requires obj') unless obj
@extend(obj)
for key, value of obj.prototype when key not in moduleKeywords
@::[key] = value
included = obj.included
included.apply(this) if included
@
@extend: (obj) ->
throw new Error('extend(obj) requires obj') unless obj
for key, value of obj when key not in moduleKeywords
@[key] = value
extended = obj.extended
extended.apply(this) if extended
@
@new: ->
new @(arguments...)
@instance_methods: ->
result = []
result.push(key) for key of @prototype
result
@class_methods: ->
result = []
result.push(key) for key of @
result
instance_exec: ->
arguments[0].apply(@, arguments[1..-1]...)
instance_eval: (block) ->
block.apply(@)
send: (method) ->
if @[method]
@[method].apply(arguments...)
else
@methodMissing(arguments...) if @methodMissing
methodMissing: (method) ->
# add it to the function prototype!
for key, value of Class
Function.prototype[key] = value
exports = module.exports = Class if exports?
namespace = if global? then global else window
namespace.Class = Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment