Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'johnson'
- require 'spec'
- RUNTIME = Johnson::Runtime.new
- RUNTIME.evaluate <<-JS
- var Person = function(name) {
- this.name = name;
- this.foods = [];
- }
- Person.prototype.meet = function() {
- return "Hello there! My name is " + this.name + '.';
- }
- Person.prototype.greet = function(friend) {
- return "Hello there " + friend + "! How are you?";
- }
- Person.prototype.eats = function(foods) {
- this.foods = arguments;
- }
- JS
- module JSClass
- def self.create(name)
- klass = RUNTIME.evaluate(name)
- klass._klass_name = name
- klass.extend(ClassMethods)
- end
- module ClassMethods
- def new(*args)
- js_args = args.map { |a| a.inspect }.join(', ')
- RUNTIME.evaluate("new #{_klass_name}(#{js_args})")
- end
- end
- end
- Person = JSClass.create('Person')
- describe Person do
- before(:each) do
- @p = Person.new("Pat")
- end
- it "should meet" do
- @p.meet.should == "Hello there! My name is Pat."
- end
- it "should greet" do
- @p.greet("Buddy").should == "Hello there Buddy! How are you?"
- end
- it "should work with multiple args" do
- @p.foods.length.should == 0
- @p.eats("Fish", "Cider", "Eggs")
- @p.foods.length.should == 3
- end
- end
Add Comment
Please, Sign In to add comment