Permalink
Browse files

First version

  • Loading branch information...
0 parents commit 0e794e3a5b33babd67c37408725555bd2c9941dd Timothy Totten committed Nov 6, 2012
Showing with 165 additions and 0 deletions.
  1. +7 −0 META.info
  2. +31 −0 README.md
  3. +17 −0 lib/Method/Modifiers.pm6
  4. +20 −0 lib/Method/Modifiers/Augment.pm6
  5. +16 −0 lib/Method/Modifiers/Role.pm6
  6. +35 −0 t/01-functions.t
  7. +39 −0 t/02-augment.t
@@ -0,0 +1,7 @@
+{
+ "name" : "Method::Modifiers",
+ "version" : "*",
+ "description" : "Adds before, after and around functions to make wrapping method calls easier.",
+ "depends" : [],
+ "source-url" : "git://github.com/supernovus/perl6-method-modifiers.git"
+}
@@ -0,0 +1,31 @@
+# Method::Modifiers
+
+## Introduction
+
+Implements before(), after() and around() functions that can be used to
+modify class methods similarly to Perl 5's Moose. It uses wrap() internally,
+and returns the wrapper handler, so it is easy to .restore() the original.
+
+Has an optional augmentation version that adds the aforementioned functions
+as methods to the Any class. Mostly just a candy coating, and not really
+recommended. Use the functions, they're straightforward and don't modify
+the base classes.
+
+## Examples
+
+See the tests in "t/" for a few good examples.
+
+## Status
+
+The basic function version works. The augment version doesn't due to a
+Rakudo bug. That's okay, as I mentioned above, the augment version isn't
+really recommended anyway.
+
+## Author
+
+[Timothy Totten](https://github.com/supernovus/)
+
+## License
+
+[Artistic License 2.0](http://www.perlfoundation.org/artistic_license_2_0)
+
@@ -0,0 +1,17 @@
+module Method::Modifiers;
+
+sub around ($class, $method-name, &closure) is export
+{
+ $class.^find_method($method-name).wrap(method { closure(); });
+}
+
+sub before ($class, $method-name, &closure) is export
+{
+ $class.^find_method($method-name).wrap(method { closure(); nextsame; });
+}
+
+sub after ($class, $method-name, &closure) is export
+{
+ $class.^find_method($method-name).wrap(method { my \result = callsame; closure(); result; });
+}
+
@@ -0,0 +1,20 @@
+use Method::Modifiers;
+
+use MONKEY_TYPING;
+
+augment class Any
+{
+ method before ($method-name, &closure)
+ {
+ before(self, $method-name, &closure);
+ }
+ method after ($method-name, &closure)
+ {
+ after(self, $method-name, &closure);
+ }
+ method around ($method-name, &closure)
+ {
+ around(self, $method-name, &closure);
+ }
+}
+
@@ -0,0 +1,16 @@
+use Method::Modifiers;
+
+role Method::Modifiers::Role;
+
+method before ($method-name, &closure)
+{
+ before(self, $method-name, &closure);
+}
+method after ($method-name, &closure)
+{
+ after(self, $method-name, &closure);
+}
+method around ($method-name, &closure)
+{
+ around(self, $method-name, &closure);
+}
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+BEGIN @*INC.push: './lib';
+
+use Test;
+use Method::Modifiers;
+
+plan 10;
+
+my $called-chars = False;
+my $orig = before(Str, 'chars', { $called-chars = True; });
+my $chars = "hello".chars;
+
+is $chars, 5, 'chars returned properly';
+ok $called-chars, 'wrapper was called';
+ok $orig.restore, 'we restored the original';
+
+$called-chars = False;
+$orig = after(Str, 'chars', { $called-chars = True });
+$chars = "test".chars;
+is $chars, 4, 'chars returned properly';
+ok $called-chars, 'wrapper was called';
+ok $orig.restore, 'we restored the original';
+
+$orig = around(Str, 'chars', { '<chars>'~callsame~'</chars>' });
+$chars = "goodbye".chars;
+is $chars, '<chars>7</chars>', 'overridden chars returned properly';
+ok $orig.restore, 'we restored the original';
+
+$called-chars = False;
+$chars = "mu".chars;
+is $chars, 2, 'original chars returns properly';
+ok !$called-chars, 'no former modifiers being called';
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+BEGIN @*INC.push: './lib';
+
+use Test;
+use Method::Modifiers::Augment;
+
+plan 10;
+
+skip_rest('currently not working in rakudo due to rakudobug');
+exit;
+
+my $called-chars = False;
+my $orig = Str.before('chars', { $called-chars = True; });
+my $chars = "hello".chars;
+
+is $chars, 5, 'chars returned properly';
+ok $called-chars, 'wrapper was called';
+ok $orig.restore, 'we restored the original';
+
+$called-chars = False;
+$orig = Str.after('chars', { $called-chars = True });
+$chars = "test".chars;
+is $chars, 4, 'chars returned properly';
+ok $called-chars, 'wrapper was called';
+ok $orig.restore, 'we restored the original';
+
+$orig = Str.around('chars', { '<chars>'~callsame~'</chars>' });
+$chars = "goodbye".chars;
+is $chars, '<chars>7</chars>', 'overridden chars returned properly';
+ok $orig.restore, 'we restored the original';
+
+$called-chars = False;
+$chars = "mu".chars;
+is $chars, 2, 'original chars returns properly';
+ok !$called-chars, 'no former modifiers being called';
+

0 comments on commit 0e794e3

Please sign in to comment.