add BUILDALL support
[gitmo/Moo.git] / lib / Method / Generate / BuildAll.pm
diff --git a/lib/Method/Generate/BuildAll.pm b/lib/Method/Generate/BuildAll.pm
new file mode 100644 (file)
index 0000000..b7895b8
--- /dev/null
@@ -0,0 +1,21 @@
+package Method::Generate::BuildAll;
+
+use strictures 1;
+use base qw(Class::Tiny::Object);
+use Sub::Quote;
+use Class::Tiny::_mro;
+use Class::Tiny::_Utils;
+
+sub generate_method {
+  my ($self, $into) = @_;
+  my @builds =
+    grep *{_getglob($_)}{CODE},
+    map "${_}::BUILD",
+    reverse @{mro::get_linear_isa($into)};
+  quote_sub "${into}::BUILDALL", join '',
+    qq{    my \$self = shift;\n},
+    (map qq{    \$self->${_}(\@_);\n}, @builds),
+    qq{    return \$self\n};
+}
+
+1;