-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
86 lines (66 loc) · 1.69 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# DSL for creating and extending classes and mixins in coffeescript
# Create a class
# Initialize properties on a per-instance basis using functions
# Here, every instance of A will have its own queue.
class A extends Class
@initialize
queue: Array # or -> []
map: Object # or -> {}
a1 = new A
a1.queue.push 'foo'
(new A).queue # empty
# Use @introduce when you intend to declare new keys
# Here, an exception is thrown since 'queue' was introduced in A
class B extends A
@introduce
queue: ->
bar: ->
# init method is called immediately after object construction with the arguments
# that were passed to the constructor:
class C extends B
init: (arg) ->
# Use @before and @after to do most of your method 'overriding'.
# The super (or whatever was at that key) is called for you, whether it exists or not.
class D extends C
@after init: (arg) ->
# Define a mixin
QueueMixin = ->
@initialize
queue: Array
@introduce push: (x) -> @queue.push x
# Extend a mixin
StackMixin = ->
@do QueueMixin
@introduce pop: -> @queue.pop()
OtherMixin = ->
class MyStack extends Class
@do StackMixin, OtherMixin #Apply mixins
# Other nice stuff: Properties, Delegation, Object.do
# Properties
class Point extends Class
@property
x:
get: -> @_x ?= 0
set: (@_x) ->
y:
get: -> @_y ?= 0
set: (@_y) ->
p = new Point
p.x() # -1
p.x 3
p.x() # 3
# Delegation
class Rect extends Class
@initialize
topleft: -> new Point
dim: -> new Point
@delegate x: 'topleft', y: 'topleft'
@delegate width: 'dim.x', height: 'dim.y'
# Object.do
# Descendents of Class implement 'do', a safe form of 'with'
r = new Rect
r.do ->
@x 3
@width 15
@dim.do ->
@y 15