Skip to content
Browse files

add support for passing blocks to Class#private:, Class#protected: & …

…Class#public:
  • Loading branch information...
1 parent 40007c5 commit 36a9971125c4e54f1adc20c5ecbe4f24203a0534 @bakkdoor committed Aug 16, 2012
Showing with 159 additions and 1 deletion.
  1. +106 −0 lib/class.fy
  2. +53 −1 tests/class.fy
View
106 lib/class.fy
@@ -8,6 +8,112 @@ class Class {
forwards_unary_ruby_methods
+ alias_method: '__private__: for: 'private:
+ alias_method: '__protected__: for: 'protected:
+ alias_method: '__public__: for: 'public:
+
+ def private: private_methods {
+ """
+ @private_methods @Block@ or @Fancy::Enumerable@ of method names to be private.
+
+ Sets any given method names to private on this @Class@.
+
+ Example:
+ class MyClass {
+ def foo {}
+ def bar {}
+
+ private: 'foo
+ private: 'bar
+
+ # same as:
+ private: ('foo, 'bar)
+
+ # same as:
+ private: {
+ def foo {}
+ def bar {}
+ }
+ }
+ """
+
+ __private__: $ match private_methods {
+ case Block ->
+ methods = instance_methods: false
+ private_methods call
+ instance_methods: false - methods
+ case _ -> private_methods
+ }
+ }
+
+ def protected: protected_methods {
+ """
+ @protected_methods @Block@ or @Fancy::Enumerable@ of method names to be protected.
+
+ Sets any given method names to protected on this @Class@.
+
+ Example:
+ class MyClass {
+ def foo {}
+ def bar {}
+
+ protected: 'foo
+ protected: 'bar
+
+ # same as:
+ protected: ('foo, 'bar)
+
+ # same as:
+ protected: {
+ def foo {}
+ def bar {}
+ }
+ }
+ """
+
+ __protected__: $ match protected_methods {
+ case Block ->
+ methods = instance_methods: false
+ protected_methods call
+ instance_methods: false - methods
+ case _ -> protected_methods
+ }
+ }
+
+ def public: public_methods {
+ """
+ @public_methods @Block@ or @Fancy::Enumerable@ of method names to be public.
+
+ Sets any given method names to public on this @Class@.
+
+ Example:
+ class MyClass {
+ def foo {}
+ def bar {}
+
+ public: 'foo
+ public: 'bar
+
+ # same as:
+ public: ('foo, 'bar)
+
+ # same as:
+ public: {
+ def foo {}
+ def bar {}
+ }
+ }
+ """
+
+ __public__: $ match public_methods {
+ case Block ->
+ methods = instance_methods: false
+ public_methods call
+ instance_methods: false - methods
+ case _ -> public_methods
+ }
+ }
+
def define_slot_reader: slotname {
"""
@slotname Name of the slot to define a getter method for.
View
54 tests/class.fy
@@ -473,6 +473,20 @@ FancySpec describe: Class with: {
{ x b } raises: NoMethodError
AClassWithPrivateMethods instance_method: 'a . private? is: true
AClassWithPrivateMethods instance_method: 'b . private? is: true
+
+ class AClassWithPrivateMethods {
+ private: {
+ def c {
+ "private c"
+ }
+ def d {
+ "private d"
+ }
+ }
+ }
+
+ AClassWithPrivateMethods instance_method: 'c . private? is: true
+ AClassWithPrivateMethods instance_method: 'd . private? is: true
}
it: "makes methods protected" with: 'protected: when: {
@@ -493,6 +507,25 @@ FancySpec describe: Class with: {
AClassWithProtectedMethods instance_method: 'b . private? is: false
AClassWithProtectedMethods instance_method: 'a . protected? is: true
AClassWithProtectedMethods instance_method: 'b . protected? is: true
+
+ class AClassWithProtectedMethods {
+ protected: {
+ def c {
+ "in c"
+ }
+ def d {
+ "in d"
+ }
+ }
+ }
+
+ { x a } raises: NoMethodError
+ { x b } raises: NoMethodError
+ AClassWithProtectedMethods instance_method: 'c . private? is: false
+ AClassWithProtectedMethods instance_method: 'd . private? is: false
+ AClassWithProtectedMethods instance_method: 'c . protected? is: true
+ AClassWithProtectedMethods instance_method: 'd . protected? is: true
+
}
it: "makes methods public" with: 'public: when: {
@@ -516,6 +549,26 @@ FancySpec describe: Class with: {
AClassWithPublicMethods instance_method: 'b . protected? is: false
AClassWithPublicMethods instance_method: 'a . public? is: true
AClassWithPublicMethods instance_method: 'b . public? is: true
+
+ class AClassWithPublicMethods {
+ public: {
+ def c {
+ "in c"
+ }
+ def d {
+ "in d"
+ }
+ }
+ }
+
+ { x c } does_not raise: NoMethodError
+ { x d } does_not raise: NoMethodError
+ AClassWithPublicMethods instance_method: 'c . private? is: false
+ AClassWithPublicMethods instance_method: 'd . private? is: false
+ AClassWithPublicMethods instance_method: 'c . protected? is: false
+ AClassWithPublicMethods instance_method: 'd . protected? is: false
+ AClassWithPublicMethods instance_method: 'c . public? is: true
+ AClassWithPublicMethods instance_method: 'd . public? is: true
}
it: "defines a class without a body" when: {
@@ -894,6 +947,5 @@ FancySpec describe: Class with: {
{ sa w2 } raises: NoMethodError
{ sa w2: "foo" } does_not raise: NoMethodError
-
}
}

0 comments on commit 36a9971

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