Skip to content

Commit

Permalink
Adding ConstantScope module, which simulates Ruby's constant lookup s…
Browse files Browse the repository at this point in the history
…emantics.
  • Loading branch information
jcoglan committed Aug 30, 2008
1 parent bdec1b7 commit 501c469
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jake.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ packages:
class: class class: class
package: package package: package
comparable: comparable comparable: comparable
constant_scope: constant_scope
enumerable: enumerable enumerable: enumerable
observable: observable observable: observable
forwardable: forwardable forwardable: forwardable
Expand All @@ -29,6 +30,7 @@ bundles:
- enumerable - enumerable
- observable - observable
- forwardable - forwardable
- constant_scope
- decorator - decorator
- proxy - proxy
- command - command
Expand Down
45 changes: 45 additions & 0 deletions source/constant_scope.js
Original file line number Original file line Diff line number Diff line change
@@ -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.