File tree 3 files changed +27
-2
lines changed
3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change
1
+ require 'native'
2
+
3
+ describe "Class#native_alias" do
4
+ it "exposes a method to javascript without the '$' prefix" do
5
+ klass = Class . new {
6
+ def a
7
+ 2
8
+ end
9
+
10
+ native_alias :a , :a
11
+ }
12
+ instance = klass . new
13
+ `instance.a()` . should == 2
14
+ end
15
+
16
+ it "raises if the aliased method isn't defined" do
17
+ lambda { Class . new { native_alias :a , :not_a_method } } . should raise_error ( NameError )
18
+ end
19
+ end
Original file line number Diff line number Diff line change @@ -538,8 +538,14 @@ def native_module
538
538
end
539
539
540
540
class Class
541
- def native_alias ( jsid , mid )
542
- `#{ self } .$$proto[#{ jsid } ] = #{ self } .$$proto['$' + #{ mid } ]`
541
+ def native_alias ( new_jsid , existing_mid )
542
+ %x{
543
+ var aliased = #{ self } .$$proto['$' + #{ existing_mid } ];
544
+ if (!aliased) {
545
+ #{ raise NameError , "undefined method `#{ existing_mid } ' for class `#{ inspect } '" } ;
546
+ }
547
+ #{ self } .$$proto[#{ new_jsid } ] = aliased;
548
+ }
543
549
end
544
550
545
551
alias native_class native_module
You can’t perform that action at this time.
1 commit comments
matthewp commentedon Jan 26, 2015
Good change!