Skip to content
Browse files

Interop API to seamlessly call noscript from Ruby

  • Loading branch information...
1 parent d59cf80 commit 8cc3da55c7ea512b5e21f886d3454e278e43b8b7 @txus committed Jan 27, 2012
Showing with 30 additions and 4 deletions.
  1. +12 −4 lib/noscript/runtime.rb
  2. +18 −0 test/interop_test.rb
View
16 lib/noscript/runtime.rb
@@ -72,10 +72,18 @@ def __noscript_put__(name, object)
end
def method_missing(m, *args)
- function = m.to_s.split(":").last.to_sym
- if __noscript_has_property__(function)
- fn = __noscript_get__(function)
- return fn.call(*args) # First arg is this.
+ from_ruby = !(m =~ /^noscript:/)
+ name = from_ruby ? m : m.to_s.split(":").last.to_sym
+ if __noscript_has_property__(name)
+ property = __noscript_get__(name)
+ if property.is_a?(Function)
+ if from_ruby
+ args.unshift self
+ end
+ return property.call(*args) # First arg is this.
+ else
+ return property
+ end
end
super
end
View
18 test/interop_test.rb
@@ -78,4 +78,22 @@ def test_create_ruby_class
assert_respond_to foo, :answer
assert_equal 42, foo.answer
end
+
+ def test_call_noscript_from_ruby
+ john = compile(<<-CODE)
+ Object.clone({
+ name: 'John',
+ age: ->
+ 20
+ end,
+ money: -> day of month
+ 30 - day of month
+ end
+ })
+ CODE
+
+ assert_equal "John", john.name
+ assert_equal 20, john.age
+ assert_equal 10, john.money(20)
+ end
end

0 comments on commit 8cc3da5

Please sign in to comment.
Something went wrong with that request. Please try again.