Permalink
Browse files

Adding ConstantScope module, which simulates Ruby's constant lookup s…

…emantics.
  • Loading branch information...
1 parent bdec1b7 commit 501c469b0aac11be57df04f4599a056ed25a35a4 @jcoglan jcoglan committed Aug 30, 2008
Showing with 47 additions and 0 deletions.
  1. +2 −0 jake.yml
  2. +45 −0 source/constant_scope.js
View
@@ -9,6 +9,7 @@ packages:
class: class
package: package
comparable: comparable
+ constant_scope: constant_scope
enumerable: enumerable
observable: observable
forwardable: forwardable
@@ -29,6 +30,7 @@ bundles:
- enumerable
- observable
- forwardable
+ - constant_scope
- decorator
- proxy
- command
@@ -0,0 +1,45 @@
+JS.ConstantScope = new JS.Module({
+ extend: {
+ included: function(base) {
+ base.__consts__ = new JS.Module();
+ base.extend(this.ClassMethods);
+ base.include(base.__consts__);
+ base.extend(base.__consts__);
+ },
+
+ ClassMethods: new JS.Module({
+ extend: function() {
+ var constants = JS.ConstantScope.extract(arguments[0], this);
+ this.__consts__.include(constants);
+ this.callSuper();
+ },
+
+ include: function() {
+ var constants = JS.ConstantScope.extract(arguments[0], this);
+ this.__consts__.include(constants);
+ this.callSuper();
+ }
+ }),
+
+ extract: function(inclusions, base) {
+ if (!inclusions) return null;
+ if (inclusions.isA && inclusions.isA(JS.Module)) return null;
+ var constants = {}, key, object;
+ for (key in inclusions) {
+
+ if (!/^[A-Z]/.test(key)) continue;
+
+ object = inclusions[key];
+ constants[key] = object;
+ delete inclusions[key];
+
+ if (object.isA && object.isA(JS.Module)) {
+ object.include(this);
+ object.include(base.__consts__);
+ object.extend(base.__consts__);
+ }
+ }
+ return constants;
+ }
+ }
+});

0 comments on commit 501c469

Please sign in to comment.