Subject: | Patch to enable generation of Moose-extendable classes |
Hi!
I really like to base my Result/Schema/ResultSet classes on Moose, so I
can use roles etc to reuse behaviour.
Attached is a simple patch that adds a new loader_options named
'use_moose'. If you enable this options, classes generated via
DBIC::Schema::Loader will be extendable via Moose.
A Result class will look like this:
--> start
package Oe1::Schema::Result::Newsletter;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
use strict;
use warnings;
use Moose;
use MooseX::NonMoose;
extends 'DBIx::Class::Core';
# ...
# You can replace this text with custom content, and it will be
preserved on regeneration
no Moose;
__PACKAGE__->meta->make_immutable( inline_constructor => 0 );
1;
<-- end
I'm using classes like that quite a lot for http://oe1.orf.at , they
work well & I'm rather happy with them.
If you like my patch, please apply it! If there are any issues I've
overseen or you're not happy with my naming ('use_moose', ..), please
contact me and I'll fix it according to your wishes (or just fix it
yourself :-)
Thanks for your code,
greeting,
Thomas Klausner
Subject: | use_moose.patch |
From a61f77e11443ec27cb46623d48a404366abb2850 Mon Sep 17 00:00:00 2001
From: Thomas Klausner <domm@plix.at>
Date: Mon, 31 May 2010 21:15:55 +0200
Subject: [PATCH 1/2] add attr "use_moose" to generate Schema/Result classes that are extentable via Moose
---
lib/DBIx/Class/Schema/Loader/Base.pm | 31 +++++++++++++++++++++++--------
1 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm
index 2273d03..a3eb7d8 100644
--- a/lib/DBIx/Class/Schema/Loader/Base.pm
+++ b/lib/DBIx/Class/Schema/Loader/Base.pm
@@ -50,6 +50,7 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
default_resultset_class
schema_base_class
result_base_class
+ use_moose
overwrite_modifications
relationship_attrs
@@ -1163,8 +1164,13 @@ sub _dump_to_dir {
qq|package $schema_class;\n\n|
. qq|# Created by DBIx::Class::Schema::Loader\n|
. qq|# DO NOT MODIFY THE FIRST PART OF THIS FILE\n\n|
- . qq|use strict;\nuse warnings;\n\n|
- . qq|use base '$schema_base_class';\n\n|;
+ . qq|use strict;\nuse warnings;\n\n|;
+ if ($self->use_moose) {
+ $schema_text.= qq|use Moose;\nuse MooseX::NonMoose;\nextends '$schema_base_class';\n\n|;
+ }
+ else {
+ $schema_text .= qq|use base '$schema_base_class';\n\n|;
+ }
if ($self->use_namespaces) {
$schema_text .= qq|__PACKAGE__->load_namespaces|;
@@ -1198,9 +1204,13 @@ sub _dump_to_dir {
qq|package $src_class;\n\n|
. qq|# Created by DBIx::Class::Schema::Loader\n|
. qq|# DO NOT MODIFY THE FIRST PART OF THIS FILE\n\n|
- . qq|use strict;\nuse warnings;\n\n|
- . qq|use base '$result_base_class';\n\n|;
-
+ . qq|use strict;\nuse warnings;\n\n|;
+ if ($self->use_moose) {
+ $src_text.= qq|use Moose;\nuse MooseX::NonMoose;\nextends '$result_base_class';\n\n|;
+ }
+ else {
+ $src_text .= qq|use base '$result_base_class';\n\n|;
+ }
$self->_write_classfile($src_class, $src_text);
}
@@ -1304,9 +1314,14 @@ sub _write_classfile {
}
sub _default_custom_content {
- return qq|\n\n# You can replace this text with custom|
- . qq| content, and it will be preserved on regeneration|
- . qq|\n1;\n|;
+ my $self = shift;
+ my $default = qq|\n\n# You can replace this text with custom|
+ . qq| content, and it will be preserved on regeneration|;
+ if ($self->use_moose) {
+ $default .= qq|\nno Moose;\n__PACKAGE__->meta->make_immutable( inline_constructor => 0 );\n1;\n|;
+ }
+ $default .= qq|\n1;\n|;
+ return $default;
}
sub _get_custom_content {
--
1.6.5
From cbbc806d29f2234538601f0794fccb792aa2a0fd Mon Sep 17 00:00:00 2001
From: Thomas Klausner <domm@plix.at>
Date: Mon, 31 May 2010 21:16:21 +0200
Subject: [PATCH 2/2] very simply test for use_moose (only proves that nothing breaks)
---
t/20invocations.t | 9 +++++++++
1 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/t/20invocations.t b/t/20invocations.t
index b02402f..fe52379 100644
--- a/t/20invocations.t
+++ b/t/20invocations.t
@@ -124,6 +124,15 @@ my @invocations = (
);
DBICTest::Schema::14->clone;
},
+ 'moose' => sub {
+ package DBICTest::Schema::8;
+ use base qw/ DBIx::Class::Schema::Loader /;
+ __PACKAGE__->naming('current');
+ __PACKAGE__->connect(
+ $make_dbictest_db::dsn,
+ { loader_options => { use_moose => 1 } }
+ );
+ },
);
# 4 tests per k/v pair
--
1.6.5