cornerstone-source 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +20 -0
  5. data/Rakefile +8 -0
  6. data/config.rb +79 -0
  7. data/config.ru +4 -0
  8. data/cornerstone.gemspec +22 -0
  9. data/doc_scraper.rb +51 -0
  10. data/game.js +4293 -0
  11. data/lib/assets/javascripts/cornerstone.js +4719 -0
  12. data/lib/cornerstone.rb +11 -0
  13. data/lib/cornerstone/rails.rb +5 -0
  14. data/lib/cornerstone/sprockets.rb +2 -0
  15. data/lib/cornerstone/version.rb +3 -0
  16. data/manifest.json +15 -0
  17. data/pixie.json +12 -0
  18. data/source/javascripts/_cornerstone/_object_extensions.js.coffee +108 -0
  19. data/source/javascripts/_cornerstone/array_extensions.js.coffee +570 -0
  20. data/source/javascripts/_cornerstone/bindable.js.coffee +125 -0
  21. data/source/javascripts/_cornerstone/command_stack.js.coffee +36 -0
  22. data/source/javascripts/_cornerstone/core_object.js.coffee +183 -0
  23. data/source/javascripts/_cornerstone/function_extensions.js.coffee +60 -0
  24. data/source/javascripts/_cornerstone/logging.js.coffee +19 -0
  25. data/source/javascripts/_cornerstone/matrix.js.coffee +337 -0
  26. data/source/javascripts/_cornerstone/number_extensions.js.coffee +491 -0
  27. data/source/javascripts/_cornerstone/point.js.coffee +641 -0
  28. data/source/javascripts/_cornerstone/random.js.coffee +86 -0
  29. data/source/javascripts/_cornerstone/rectangle.js.coffee +35 -0
  30. data/source/javascripts/_cornerstone/string_extensions.js.coffee +232 -0
  31. data/source/javascripts/_cornerstone/stubs.js.coffee +1042 -0
  32. data/source/javascripts/_cornerstone/uuid.js +96 -0
  33. data/source/javascripts/_test/array_extensions.coffee +173 -0
  34. data/source/javascripts/_test/bindable.coffee +68 -0
  35. data/source/javascripts/_test/command_stack.coffee +99 -0
  36. data/source/javascripts/_test/core_object.coffee +95 -0
  37. data/source/javascripts/_test/function_extensions.coffee +50 -0
  38. data/source/javascripts/_test/logging.coffee +7 -0
  39. data/source/javascripts/_test/matrix.coffee +174 -0
  40. data/source/javascripts/_test/number_extensions.coffee +138 -0
  41. data/source/javascripts/_test/object_extensions.coffee +53 -0
  42. data/source/javascripts/_test/point.coffee +196 -0
  43. data/source/javascripts/_test/random.coffee +22 -0
  44. data/source/javascripts/_test/rectangle.coffee +70 -0
  45. data/source/javascripts/_test/string_extensions.coffee +59 -0
  46. data/source/javascripts/cornerstone.js.coffee +1 -0
  47. data/source/javascripts/cornerstone_tests.js.coffee +2 -0
  48. data/source/test.html.haml +13 -0
  49. data/vendor/javascripts/qunit.js +1275 -0
  50. data/vendor/stylesheets/qunit.css.sass +115 -0
  51. metadata +141 -0
@@ -0,0 +1,491 @@
1
+ ###*
2
+ Returns the absolute value of this number.
3
+
4
+ (-4).abs()
5
+ # => 4
6
+
7
+ @name abs
8
+ @methodOf Number#
9
+ @returns {Number} The absolute value of the number.
10
+ ###
11
+ Number::abs = () ->
12
+ Math.abs(this)
13
+
14
+ ###*
15
+ Returns the mathematical ceiling of this number.
16
+
17
+ 4.9.ceil()
18
+ # => 5
19
+
20
+ 4.2.ceil()
21
+ # => 5
22
+
23
+ (-1.2).ceil()
24
+ # => -1
25
+
26
+ @name ceil
27
+ @methodOf Number#
28
+ @returns {Number} The number truncated to the nearest integer of greater than or equal value.
29
+ ###
30
+ Number::ceil = ->
31
+ Math.ceil(this)
32
+
33
+ ###*
34
+ Returns the mathematical floor of this number.
35
+
36
+ 4.9.floor()
37
+ # => 4
38
+
39
+ 4.2.floor()
40
+ # => 4
41
+
42
+ (-1.2).floor()
43
+ # => -2
44
+
45
+ @name floor
46
+ @methodOf Number#
47
+ @returns {Number} The number truncated to the nearest integer of less than or equal value.
48
+ ###
49
+ Number::floor = ->
50
+ Math.floor(this)
51
+
52
+ ###*
53
+ Returns this number rounded to the nearest integer.
54
+
55
+ 4.5.round()
56
+ # => 5
57
+
58
+ 4.4.round()
59
+ # => 4
60
+
61
+ @name round
62
+ @methodOf Number#
63
+ @returns {Number} The number rounded to the nearest integer.
64
+ ###
65
+ Number::round = ->
66
+ Math.round(this)
67
+
68
+ ###*
69
+ Get a bunch of points equally spaced around the unit circle.
70
+
71
+ 4.circularPoints (p) ->
72
+
73
+ # p gets Point(1, 0), Point(0, 1), Point(-1, 0), Point(0, -1)
74
+
75
+ @name circularPoint
76
+ @methodOf Number#
77
+
78
+ ###
79
+ Number::circularPoints = (block) ->
80
+ n = this
81
+
82
+ n.times (i) ->
83
+ block(Point.fromAngle((i/n).turns), i)
84
+
85
+ ###*
86
+ Returns a number whose value is limited to the given range.
87
+
88
+ # limit the output of this computation to between 0 and 255
89
+ (2 * 255).clamp(0, 255)
90
+ # => 255
91
+
92
+ @name clamp
93
+ @methodOf Number#
94
+ @param {Number} min The lower boundary of the output range
95
+ @param {Number} max The upper boundary of the output range
96
+ @returns {Number} A number in the range [min, max]
97
+ ###
98
+ Number::clamp = (min, max) ->
99
+ if min? and max?
100
+ Math.min(Math.max(this, min), max)
101
+ else if min?
102
+ Math.max(this, min)
103
+ else if max?
104
+ Math.min(this, max)
105
+ else
106
+ this
107
+
108
+ ###*
109
+ A mod method useful for array wrapping. The range of the function is
110
+ constrained to remain in bounds of array indices.
111
+
112
+ (-1).mod(5)
113
+ # => 4
114
+
115
+ @name mod
116
+ @methodOf Number#
117
+ @param {Number} base
118
+ @returns {Number} An integer between 0 and (base - 1) if base is positive.
119
+ ###
120
+ Number::mod = (base) ->
121
+ result = this % base;
122
+
123
+ if result < 0 && base > 0
124
+ result += base
125
+
126
+ return result
127
+
128
+ ###*
129
+ Get the sign of this number as an integer (1, -1, or 0).
130
+
131
+ (-5).sign()
132
+ # => -1
133
+
134
+ 0.sign()
135
+ # => 0
136
+
137
+ 5.sign()
138
+ # => 1
139
+
140
+ @name sign
141
+ @methodOf Number#
142
+ @returns {Number} The sign of this number, 0 if the number is 0.
143
+ ###
144
+ Number::sign = ->
145
+ if this > 0
146
+ 1
147
+ else if this < 0
148
+ -1
149
+ else
150
+ 0
151
+
152
+ ###*
153
+ Returns true if this number is even (evenly divisible by 2).
154
+
155
+ 2.even()
156
+ # => true
157
+
158
+ 3.even()
159
+ # => false
160
+
161
+ 0.even()
162
+ # => true
163
+
164
+ @name even
165
+ @methodOf Number#
166
+ @returns {Boolean} true if this number is an even integer, false otherwise.
167
+ ###
168
+ Number::even = ->
169
+ this % 2 == 0
170
+
171
+ ###*
172
+ Returns true if this number is odd (has remainder of 1 when divided by 2).
173
+
174
+ 2.odd()
175
+ # => false
176
+
177
+ 3.odd()
178
+ # => true
179
+
180
+ 0.odd()
181
+ # => false
182
+
183
+ @name odd
184
+ @methodOf Number#
185
+ @returns {Boolean} true if this number is an odd integer, false otherwise.
186
+ ###
187
+ Number::odd = ->
188
+ if this > 0
189
+ this % 2 == 1
190
+ else
191
+ this % 2 == -1
192
+
193
+ ###*
194
+ Calls iterator the specified number of times, passing in the number of the
195
+ current iteration as a parameter: 0 on first call, 1 on the second call, etc.
196
+
197
+ output = []
198
+
199
+ 5.times (n) ->
200
+ output.push(n)
201
+
202
+ output
203
+ # => [0, 1, 2, 3, 4]
204
+
205
+ @name times
206
+ @methodOf Number#
207
+ @param {Function} iterator The iterator takes a single parameter, the number of the current iteration.
208
+ @param {Object} [context] The optional context parameter specifies an object to treat as <code>this</code> in the iterator block.
209
+ @returns {Number} The number of times the iterator was called.
210
+ ###
211
+ Number::times = (iterator, context) ->
212
+ i = -1
213
+
214
+ while ++i < this
215
+ iterator.call context, i
216
+
217
+ return i
218
+
219
+ ###*
220
+ Returns the the nearest grid resolution less than or equal to the number.
221
+
222
+ 7.snap(8)
223
+ # => 0
224
+
225
+ 4.snap(8)
226
+ # => 0
227
+
228
+ 12.snap(8)
229
+ # => 8
230
+
231
+ @name snap
232
+ @methodOf Number#
233
+ @param {Number} resolution The grid resolution to snap to.
234
+ @returns {Number} The nearest multiple of resolution lower than the number.
235
+ ###
236
+ Number::snap = (resolution) ->
237
+ n = this / resolution; 1/1; # This is to fix broken regex in doc parser
238
+ n.floor() * resolution
239
+
240
+ ###*
241
+ In number theory, integer factorization or prime factorization is the
242
+ breaking down of a composite number into smaller non-trivial divisors,
243
+ which when multiplied together equal the original integer.
244
+
245
+ Floors the number for purposes of factorization.
246
+
247
+ 60.primeFactors()
248
+ # => [2, 2, 3, 5]
249
+
250
+ 37.primeFactors()
251
+ # => [37]
252
+
253
+ @name primeFactors
254
+ @methodOf Number#
255
+ @returns {Array} An array containing the factorization of this number.
256
+ ###
257
+ Number::primeFactors = ->
258
+ factors = []
259
+
260
+ n = Math.floor(this)
261
+
262
+ if n == 0
263
+ return undefined
264
+
265
+ if n < 0
266
+ factors.push(-1)
267
+ n /= -1
268
+
269
+ i = 2
270
+ iSquared = i * i
271
+
272
+ while iSquared < n
273
+ while (n % i) == 0
274
+ factors.push i
275
+ n /= i
276
+
277
+ i += 1
278
+ iSquared = i * i
279
+
280
+ if n != 1
281
+ factors.push n
282
+
283
+ return factors
284
+
285
+ ###*
286
+ Returns the two character hexidecimal
287
+ representation of numbers 0 through 255.
288
+
289
+ 255.toColorPart()
290
+ # => "ff"
291
+
292
+ 0.toColorPart()
293
+ # => "00"
294
+
295
+ 200.toColorPart()
296
+ # => "c8"
297
+
298
+ @name toColorPart
299
+ @methodOf Number#
300
+ @returns {String} Hexidecimal representation of the number
301
+ ###
302
+ Number::toColorPart = ->
303
+ s = parseInt(this.clamp(0, 255), 10).toString(16)
304
+
305
+ if s.length == 1
306
+ s = '0' + s
307
+
308
+ return s
309
+
310
+ ###*
311
+ Returns a number that is maxDelta closer to target.
312
+
313
+ 255.approach(0, 5)
314
+ # => 250
315
+
316
+ 5.approach(0, 10)
317
+ # => 0
318
+
319
+ @name approach
320
+ @methodOf Number#
321
+ @returns {Number} A number maxDelta toward target
322
+ ###
323
+ Number::approach = (target, maxDelta) ->
324
+ (target - this).clamp(-maxDelta, maxDelta) + this
325
+
326
+ ###*
327
+ Returns a number that is closer to the target by the ratio.
328
+
329
+ 255.approachByRatio(0, 0.1)
330
+ # => 229.5
331
+
332
+ @name approachByRatio
333
+ @methodOf Number#
334
+ @returns {Number} A number toward target by the ratio
335
+ ###
336
+ Number::approachByRatio = (target, ratio) ->
337
+ this.approach(target, this * ratio)
338
+
339
+ ###*
340
+ Returns a number that is closer to the target angle by the delta.
341
+
342
+ Math.PI.approachRotation(0, Math.PI/4)
343
+ # => 2.356194490192345 # this is (3/4) * Math.PI, which is (1/4) * Math.PI closer to 0 from Math.PI
344
+
345
+ @name approachRotation
346
+ @methodOf Number#
347
+ @returns {Number} A number toward the target angle by maxDelta
348
+ ###
349
+ Number::approachRotation = (target, maxDelta) ->
350
+ while target > this + Math.PI
351
+ target -= Math.TAU
352
+
353
+ while target < this - Math.PI
354
+ target += Math.TAU
355
+
356
+ return (target - this).clamp(-maxDelta, maxDelta) + this
357
+
358
+ ###*
359
+ Constrains a rotation to between -PI and PI.
360
+
361
+ (9/4 * Math.PI).constrainRotation()
362
+ # => 0.7853981633974483 # this is (1/4) * Math.PI
363
+
364
+ @name constrainRotation
365
+ @methodOf Number#
366
+ @returns {Number} This number constrained between -PI and PI.
367
+ ###
368
+ Number::constrainRotation = ->
369
+ target = this
370
+
371
+ while target > Math.PI
372
+ target -= Math.TAU
373
+
374
+ while target < -Math.PI
375
+ target += Math.TAU
376
+
377
+ return target
378
+
379
+ ###*
380
+ The mathematical d operator. Useful for simulating dice rolls.
381
+
382
+ @name d
383
+ @methodOf Number#
384
+ @returns {Number} The sum of rolling <code>this</code> many <code>sides</code>-sided dice
385
+ ###
386
+ Number::d = (sides) ->
387
+ sum = 0
388
+
389
+ this.times ->
390
+ sum += rand(sides) + 1
391
+
392
+ return sum
393
+
394
+ ###*
395
+ Utility method to convert a number to a duration of seconds.
396
+
397
+ 3.seconds
398
+ # => 3000
399
+
400
+ setTimout doSometing, 3.seconds
401
+
402
+ @name seconds
403
+ @propertyOf Number#
404
+ @returns {Number} This number as a duration of seconds
405
+ ###
406
+ unless 5.seconds
407
+ Object.defineProperty Number::, 'seconds',
408
+ get: ->
409
+ this * 1000
410
+
411
+ unless 1.second
412
+ Object.defineProperty Number::, 'second',
413
+ get: ->
414
+ this * 1000
415
+
416
+ ###*
417
+ Utility method to convert a number to an amount of rotations.
418
+
419
+ 0.5.rotations
420
+ # => 3.141592653589793
421
+
422
+ I.rotation = 0.25.rotations
423
+
424
+ @name rotations
425
+ @propertyOf Number#
426
+ @returns {Number} This number as an amount of rotations
427
+ ###
428
+ unless 5.rotations
429
+ Object.defineProperty Number::, 'rotations',
430
+ get: ->
431
+ this * Math.TAU
432
+
433
+ unless 1.rotation
434
+ Object.defineProperty Number::, 'rotation',
435
+ get: ->
436
+ this * Math.TAU
437
+
438
+ ###*
439
+ Utility method to convert a number to an amount of rotations.
440
+
441
+ 0.5.turns
442
+ # => 3.141592653589793
443
+
444
+ I.rotation = 0.25.turns
445
+
446
+ 1.turn # => Math.TAU (aka 2 * Math.PI)
447
+
448
+ @name turns
449
+ @propertyOf Number#
450
+ @returns {Number} This number as an amount of rotation.
451
+ 1 turn is one complete rotation.
452
+ ###
453
+ unless 5.turns
454
+ Object.defineProperty Number.prototype, 'turns',
455
+ get: ->
456
+ this * Math.TAU
457
+
458
+ unless 1.turn
459
+ Object.defineProperty Number.prototype, 'turn',
460
+ get: ->
461
+ this * Math.TAU
462
+
463
+ ###*
464
+ Utility method to convert a number to an amount of degrees.
465
+
466
+ 180.degrees
467
+ # => 3.141592653589793
468
+
469
+ I.rotation = 90.degrees
470
+
471
+ @name degrees
472
+ @propertyOf Number#
473
+ @returns {Number} This number as an amount of degrees
474
+ ###
475
+ unless 2.degrees
476
+ Object.defineProperty Number::, 'degrees',
477
+ get: ->
478
+ this * Math.TAU / 360
479
+
480
+ unless 1.degree
481
+ Object.defineProperty Number::, 'degree',
482
+ get: ->
483
+ this * Math.TAU / 360
484
+
485
+ ###*
486
+ The mathematical circle constant of 1 turn.
487
+
488
+ @name TAU
489
+ @fieldOf Math
490
+ ###
491
+ Math.TAU = 2 * Math.PI