Permalink
Browse files

Add Class#expose_to_ruby:as:

  • Loading branch information...
1 parent f7f43d5 commit aa38b96af40b5897dda6b7bd168f9482b0c77cee @bakkdoor committed Jun 18, 2012
Showing with 43 additions and 0 deletions.
  1. +27 −0 lib/rbx/class.fy
  2. +16 −0 tests/class.fy
View
@@ -231,4 +231,31 @@ class Class {
case _ -> class_eval: { str_or_block to_s eval }
}
}
+
+ def expose_to_ruby: method_name as: ruby_method_name (nil) {
+ """
+ @method_name Fancy method name to be exposed.
+ @ruby_method_name Name of method exposed to Ruby (optional).
+
+ Explicitly exposes a Fancy method to Ruby. If @ruby_method_name is
+ passed, use that name explicitly, otherwise uses @method_name.
+
+ Example:
+ class Foo {
+ def === other {
+ # ...
+ }
+
+ expose_to_ruby: '===
+
+ # if you don't want to expose it as :=== in Ruby:
+ expose_to_ruby: '=== as: 'some_other_name_for_ruby
+ }
+ """
+
+ match ruby_method_name {
+ case nil -> alias_method(method_name, method_name message_name)
+ case _ -> alias_method(ruby_method_name, method_name message_name)
+ }
+ }
}
View
@@ -797,4 +797,20 @@ FancySpec describe: Class with: {
CallingChainClass new bar: arr
arr is: ["baz", "bar"]
}
+
+ it: "exposes a Fancy method as a Ruby method to Ruby" with: 'expose_to_ruby:as: when: {
+ class ExposeToRuby {
+ def my_method { "in my_method" }
+ expose_to_ruby: 'my_method
+ def == other { true }
+ expose_to_ruby: '== as: 'ruby_==
+ }
+
+ ExposeToRuby new tap: @{
+ my_method is: "in my_method"
+ send('my_method) is: "in my_method"
+ == 1 is: true
+ send('ruby_==, 1) is: true
+ }
+ }
}

0 comments on commit aa38b96

Please sign in to comment.