Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 15, 2009
0 parents commit 4e47ece
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 0 deletions.
4 changes: 4 additions & 0 deletions History.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

=== 0.0.1 / YYYY-MM-DD

* Initial release
29 changes: 29 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

= YourLib

Description

== License

(The MIT License)

Copyright (c) 2009 Your Name <Your Email>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 changes: 51 additions & 0 deletions lib/oo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

// OO - Class - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
// Based on http://ejohn.org/blog/simple-javascript-inheritance/
// which is based on implementations by Prototype / base2

;(function(){
var global = this, initialize = true
var referencesSuper = /xyz/.test(function(){ xyz }) ? /\b__super__\b/ : /.*/

Class = function(props){
if (this == global)
return Class.extend(props)
}

Class.extend = function(props) {
var self = this
var __super__ = this.prototype

initialize = false
var prototype = new this
initialize = true

function mixin(props) {
for (var name in props)
prototype[name] =
typeof props[name] == 'function' &&
typeof __super__[name] == 'function' &&
referencesSuper.test(props[name]) ?
(function(name, fn){
return function() {
this.__super__ = __super__[name]
return fn.apply(this, arguments)
}
})(name, props[name])
: props[name]
}

mixin.call(this, props)

function Class() {
if (initialize && this.init)
this.init.apply(this, arguments)
}

Class.prototype = prototype
Class.constructor = Class
Class.extend = arguments.callee

return Class
}
})()
4 changes: 4 additions & 0 deletions spec/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

get '/lib/*' do |path|
send_file File.dirname(__FILE__) + '/../lib/' + path
end
123 changes: 123 additions & 0 deletions spec/spec.core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

describe 'Class'
describe '()'
it 'should create a class'
User = Class()
User.should.be_type 'function'
end
end

describe '.extend()'
it 'should create a class'
User = Class.extend()
User.should.be_type 'function'
end
end

describe '({ ... })'
it 'should populate prototype properties'
User = Class({ type: 'user' })
(new User).type.should.eql 'user'
end

it 'should populate prototype methods'
User = Class({ toString: function(){ return 'test' }})
(new User).toString().should.eql 'test'
end

it 'should initialize with the "init" method'
User = Class({
init: function(name) {
this.name = name
}
})
(new User('tj')).name.should.eql 'tj'
end

it 'should call the "init" method only once per initialization'
init = function(){}
User = Class({ init: init })
User.prototype.should.receive('init', 'once').with_args('tj')
new User('tj')
end

it 'should inherit properties of the super class'
User = Class({ type: 'user' })
Admin = User.extend()
(new Admin).type.should.eql 'user'
end

it 'should work when using the instanceof operator'
User = Class({ type: 'user' })
Admin = User.extend()
(new User).should.be_an_instance_of User
(new Admin).should.be_an_instance_of Admin
end

it 'should access the superclass via __super__'
User = Class({
init: function(name) {
this.name = name
}
})

Admin = User.extend({
init: function(name) {
this.__super__(name)
}
})

(new Admin('tj')).name.should.eql 'tj'
end

it 'should allow a subclass to override methods'
User = Class({
init: function(name) {
this.name = name
}
})

Admin = User.extend({
init: function(name) {
this.name = '<' + name + '>'
}
})

(new Admin('tj')).name.should.eql '<tj>'
end
it 'should allow multiple inheritance'
User = Class({
init: function(name) {
this.name = name
},
toString: function() {
return this.name
}
})
Manager = User.extend({
init: function(name) {
this.__super__(name)
this.type = 'Manager'
},
toString: function() {
return this.__super__() + ' is a ' + this.type
}
})
Admin = Manager.extend({
init: function(name) {
this.__super__(name)
this.type = 'Admin'
}
})
(new User('tj')).toString().should.eql 'tj'
(new Manager('tj')).toString().should.eql 'tj is a Manager'
(new Admin('tj')).toString().should.eql 'tj is a Admin'
end
end
end
20 changes: 20 additions & 0 deletions spec/spec.dom.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.11.1/lib/jspec.css" />
<script src="/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.11.1/lib/jspec.js"></script>
<script src="../lib/yourlib.core.js"></script>
<script>
function runSuites() {
JSpec
.exec('spec.core.js')
.run()
.report()
}
</script>
</head>
<body class="jspec" onLoad="runSuites();">
<div id="jspec-top"><h2 id="jspec-title">JSpec <em><script>document.write(JSpec.version)</script></em></h2></div>
<div id="jspec"></div>
<div id="jspec-bottom"></div>
</body>
</html>
8 changes: 8 additions & 0 deletions spec/spec.rhino.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

load('/Library/Ruby/Gems/1.8/gems/visionmedia-jspec-2.11.1/lib/jspec.js')
load('lib/oo.js')

JSpec
.exec('spec/spec.core.js')
.run({ formatter : JSpec.formatters.Terminal })
.report()
16 changes: 16 additions & 0 deletions spec/spec.server.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<script src="/jspec/jspec.js"></script>
<script src="/lib/yourlib.core.js"></script>
<script>
function runSuites() {
JSpec
.exec('spec.core.js')
.run({ formatter : JSpec.formatters.Server, verbose: true, failuresOnly: true })
.report()
}
</script>
</head>
<body class="jspec" onLoad="runSuites();">
</body>
</html>

0 comments on commit 4e47ece

Please sign in to comment.