| 1 | package MooseX::MakeImmutable; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | MooseX::MakeImmutable - A convenient way to make many Moosen immutable (or mutable) in one shot | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.02 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.02'; | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | package MyPackage; | ||
| 21 | |||
| 22 | use Moose; | ||
| 23 | use MooseX::MakeImmutable; | ||
| 24 | |||
| 25 | ... | ||
| 26 | |||
| 27 | MooseX::MakeImmutable->lock_down; | ||
| 28 | # MyPackage and any subdordinate Moose classes are made immutable | ||
| 29 | # Use MooseX::MakeImmutable->open_up to do the opposite | ||
| 30 | |||
| 31 | You can also make classes im/mutable with fine-grained control: | ||
| 32 | |||
| 33 | use MooseX::MakeImmutable; | ||
| 34 | |||
| 35 | ... | ||
| 36 | |||
| 37 | my $manifest = <<_MANIFEST_; | ||
| 38 | |||
| 39 | My::Moose::Hierarchy::Alpha | ||
| 40 | My::Moose::Hierarchy::Bravo | ||
| 41 | My::Moose::Hierarchy::Charlie | ||
| 42 | |||
| 43 | # Comments (lines leading with a pound) and blank lines are ignored by the finder | ||
| 44 | My::Moose::Hierarchy::Delta::* | ||
| 45 | # Not strict about leading/trailing whitespace either | ||
| 46 | My::Moose::Hierarchy::Epsilon::+ | ||
| 47 | |||
| 48 | _MANIFEST_ | ||
| 49 | |||
| 50 | MooseX::MakeImmutable->make_immutable($manifest); | ||
| 51 | |||
| 52 | # The above code has the following effects: | ||
| 53 | # | ||
| 54 | # ::Alpha, ::Bravo, and ::Charlie are now immutable (if they exist) | ||
| 55 | # | ||
| 56 | # Every Moose::Object under the Delta:: namespace is now immutable | ||
| 57 | # (although ::Delta, if a Moose::Object, IS still mutable) | ||
| 58 | # | ||
| 59 | # Every Moose::Object under the Epsilon:: namespace, including | ||
| 60 | # ::Epsilon is now mutable | ||
| 61 | |||
| 62 | # You can also use MooseX::MakeImmutable to make something mutable again: | ||
| 63 | MooseX::MakeImmutable->make_mutable("My::Moose::Hierarchy::Epsilon::+") | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | MooseX::MakeImmutable is a tool for loading every Moose::Object within a hierarchy and making each immutable/mutable. It uses L<Module::Pluggable> for searching and will load both inner and .pm packages. | ||
| 68 | |||
| 69 | In a nutshell, if you add a Moose-based package to your object hierarchy, then MooseX::MakeImmutable, given a proper manifest, will pick it up and mark it im/mutable (without you having to manually write-out the new package). | ||
| 70 | |||
| 71 | NOTE: The name "MakeImmutable" is a bit of a misnomer, since this package can both make_immutable AND make_mutable. However, 90% of the time, you'll probably be using ->make_immutable | ||
| 72 | |||
| 73 | =head2 Writing a MooseX::MakeImmutable::Finder manifest | ||
| 74 | |||
| 75 | A manifest consists of one package per line | ||
| 76 | |||
| 77 | For each line, leading and trailing whitespace is stripped | ||
| 78 | |||
| 79 | Lines that are blank or begin with a pound (#) are skipped | ||
| 80 | |||
| 81 | A package with a trailing ::* IS NOT made im/mutable, but every package under that namespace is | ||
| 82 | |||
| 83 | A package with a trailing ::+ or :: IS made im/mutable, along with every package under that namespace | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | =head2 MooseX::MakeImmutable->lock_down( [ package => <package> ], ... ) | ||
| 88 | |||
| 89 | Make everything immutable from <package> on downward | ||
| 90 | |||
| 91 | If <package> is not specified, then the package will be gotten from caller. This means you can do something like: | ||
| 92 | |||
| 93 | package MyPackage; | ||
| 94 | |||
| 95 | use Moose; | ||
| 96 | use MooseX::MakeImmutable; | ||
| 97 | |||
| 98 | ... | ||
| 99 | |||
| 100 | MooseX::MakeImmutable->lock_down; | ||
| 101 | # Effectively the same as MooseX::MakeImmutable->make_immutable('MyPackage::+'); | ||
| 102 | |||
| 103 | Any remaining arguments will be passed through to ->make_immutable | ||
| 104 | |||
| 105 | =head2 MooseX::MakeImmutable->open_up( [ package => <package> ], ... ) | ||
| 106 | |||
| 107 | Make everything mutable from <package> on downward | ||
| 108 | |||
| 109 | If <package> is not specified, then the package will be gotten from caller. This means you can do something like: | ||
| 110 | |||
| 111 | package MyPackage; | ||
| 112 | |||
| 113 | use Moose; | ||
| 114 | use MooseX::MakeImmutable; | ||
| 115 | |||
| 116 | ... | ||
| 117 | |||
| 118 | MooseX::MakeImmutable->open_up; | ||
| 119 | # Effectively the same as MooseX::MakeImmutable->make_mutable('MyPackage::+'); | ||
| 120 | |||
| 121 | Any remaining arguments will be passed through to ->make_mutable | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | =head2 MooseX::MakeImmutable->freeze( <manifest>, ... ) | ||
| 126 | |||
| 127 | =head2 MooseX::MakeImmutable->make_immutable( <manifest>, ... ) | ||
| 128 | |||
| 129 | Create a finder from <manifest> and make each found Moose::Object immutable | ||
| 130 | |||
| 131 | Any extra passed-in options will be forwarded to ->meta->make_immutable(...) excepting C<include_inner> and C<exclude>, which are used to configure the finder. | ||
| 132 | |||
| 133 | C<freeze> is an alias for C<make_immutable> | ||
| 134 | |||
| 135 | =head2 MooseX::MakeImmutable->thaw( <manifest>, ... ) | ||
| 136 | |||
| 137 | =head2 MooseX::MakeImmutable->make_mutable( <manifest>, ... ) | ||
| 138 | |||
| 139 | Create a finder from <manifest> and make each found Moose::Object mutable | ||
| 140 | |||
| 141 | Any extra passed-in options will be forwarded to ->meta->make_mutable(...) excepting C<include_inner> and C<exclude>, which are used to configure the finder. | ||
| 142 | |||
| 143 | C<thaw> is an alias for C<make_mutable> | ||
| 144 | |||
| 145 | =head2 MooseX::MakeImmutable->finder( ... ) | ||
| 146 | |||
| 147 | Create and return a MooseX::MakeImmutable::Finder object | ||
| 148 | |||
| 149 | The returned object uses L<Module::Pluggable> to scan the specified namespace(s) for potential Moose objects. It accepts the following options: | ||
| 150 | |||
| 151 | manifest The finder manifest, described above | ||
| 152 | |||
| 153 | include_inner If true, then the finder will "find" inner Moose packages. On by default | ||
| 154 | |||
| 155 | exclude A list where each item is one of: | ||
| 156 | |||
| 157 | * A package name to be excluded (string) | ||
| 158 | * A regular expression that matches if a package should be excluded | ||
| 159 | * A CODE block returning true if a package should be excluded (the package name is passed in as the first argument) | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | use MooseX::MakeImmutable::Finder; | ||
| 164 | use Scalar::Util qw/blessed/; | ||
| 165 | use Carp::Clan qw/^MooseX::MakeImmutable/; | ||
| 166 | |||
| 167 | sub lock_down { | ||
| 168 | my $class = shift; | ||
| 169 | my %given = @_; | ||
| 170 | |||
| 171 | my $package = delete $given{package}; | ||
| 172 | ($package) = caller unless defined $package; | ||
| 173 | croak "Can't lock down main::" if $package eq "main"; # Moose doesn't, why should we? | ||
| 174 | my $manifest = "${package}::+"; | ||
| 175 | $class->make_immutable($manifest, %_); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub open_up { | ||
| 179 | my $class = shift; | ||
| 180 | my %given = @_; | ||
| 181 | |||
| 182 | my $package = delete $given{package}; | ||
| 183 | ($package) = caller unless defined $package; | ||
| 184 | croak "Can't open up main::" if $package eq "main"; # Moose doesn't, why should we? | ||
| 185 | my $manifest = "${package}::+"; | ||
| 186 | $class->make_mutable($manifest, %_); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub finder { | ||
| 190 | my $class = shift; | ||
| 191 | my $finder = MooseX::MakeImmutable::Finder->new(@_); | ||
| 192 | return $finder; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub _given_finder { | ||
| 196 | my $class = shift; | ||
| 197 | my $given = shift; | ||
| 198 | |||
| 199 | my %finder; | ||
| 200 | exists $given->{$_} and $finder{$_} = delete $given->{$_} for qw/include_inner exclude/; | ||
| 201 | my $finder = delete $given->{finder}; | ||
| 202 | |||
| 203 | # If finder is already blessed... then just ignore manifest => ... and use the given finder | ||
| 204 | $finder = $class->finder((ref $finder eq "HASH" ? %$finder : ()), %finder, @_) unless blessed $finder; | ||
| 205 | |||
| 206 | return $finder; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub make_immutable { | ||
| 210 | my $class = shift; | ||
| 211 | my $manifest = shift; | ||
| 212 | my %given = @_; | ||
| 213 | |||
| 214 | my $finder = $class->_given_finder(\%given, manifest => $manifest); | ||
| 215 | |||
| 216 | $_->meta->make_immutable(%given) for $finder->found; | ||
| 217 | } | ||
| 218 | |||
| 219 | sub freeze { | ||
| 220 | return shift->make_immutable(@_); | ||
| 221 | } | ||
| 222 | |||
| 223 | sub make_mutable { | ||
| 224 | my $class = shift; | ||
| 225 | my $manifest = shift; | ||
| 226 | my %given = @_; | ||
| 227 | |||
| 228 | my $finder = $class->_given_finder(\%given, manifest => $manifest); | ||
| 229 | |||
| 230 | $_->meta->make_mutable(%given) for $finder->found; | ||
| 231 | } | ||
| 232 | |||
| 233 | sub thaw { | ||
| 234 | return shift->make_immutable(@_); | ||
| 235 | } | ||
| 236 | |||
| 237 | =head1 SEE ALSO | ||
| 238 | |||
| 239 | L<Moose> | ||
| 240 | |||
| 241 | =head1 AUTHOR | ||
| 242 | |||
| 243 | Robert Krimen, C<< <rkrimen at cpan.org> >> | ||
| 244 | |||
| 245 | =head1 SOURCE | ||
| 246 | |||
| 247 | You can contribute or fork this project via GitHub: | ||
| 248 | |||
| 249 | L<http://github.com/robertkrimen/moosex-makeimmutable/tree/master> | ||
| 250 | |||
| 251 | git clone git://github.com/robertkrimen/moosex-makeimmutable.git MooseX-MakeImmutable | ||
| 252 | |||
| 253 | =head1 BUGS | ||
| 254 | |||
| 255 | Please report any bugs or feature requests to C<bug-moosex-mutate at rt.cpan.org>, or through | ||
| 256 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-MakeImmutable>. I will be notified, and then you'll | ||
| 257 | automatically be notified of progress on your bug as I make changes. | ||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | =head1 SUPPORT | ||
| 263 | |||
| 264 | You can find documentation for this module with the perldoc command. | ||
| 265 | |||
| 266 | perldoc MooseX::MakeImmutable | ||
| 267 | |||
| 268 | |||
| 269 | You can also look for information at: | ||
| 270 | |||
| 271 | =over 4 | ||
| 272 | |||
| 273 | =item * RT: CPAN's request tracker | ||
| 274 | |||
| 275 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-MakeImmutable> | ||
| 276 | |||
| 277 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 278 | |||
| 279 | L<http://annocpan.org/dist/MooseX-MakeImmutable> | ||
| 280 | |||
| 281 | =item * CPAN Ratings | ||
| 282 | |||
| 283 | L<http://cpanratings.perl.org/d/MooseX-MakeImmutable> | ||
| 284 | |||
| 285 | =item * Search CPAN | ||
| 286 | |||
| 287 | L<http://search.cpan.org/dist/MooseX-MakeImmutable> | ||
| 288 | |||
| 289 | =back | ||
| 290 | |||
| 291 | |||
| 292 | =head1 ACKNOWLEDGEMENTS | ||
| 293 | |||
| 294 | |||
| 295 | =head1 COPYRIGHT & LICENSE | ||
| 296 | |||
| 297 | Copyright 2008 Robert Krimen, all rights reserved. | ||
| 298 | |||
| 299 | This program is free software; you can redistribute it and/or modify it | ||
| 300 | under the same terms as Perl itself. | ||
| 301 | |||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | 1; # End of MooseX::MakeImmutable |