| 1 | package MooseX::Role::Defaultable; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | MooseX::Role::Defaultable - "factory reset" for a Moose class | ||||||||||
| 6 | |||||||||||
| 7 | =cut | ||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Sub::IsEqual qw{is_equal}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | =head1 VERSION | ||||||||||
| 17 | |||||||||||
| 18 | Version 1.01 | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | 1 | our $VERSION = '1.01'; | |||||||||
| 23 | |||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||
| 25 | |||||||||||
| 26 | This module provides methods to verify and restore default values for a | ||||||||||
| 27 | Moose object's attributes, essentially providing a "factory reset" option. | ||||||||||
| 28 | |||||||||||
| 29 | Example usage: | ||||||||||
| 30 | |||||||||||
| 31 | package Foo; | ||||||||||
| 32 | use Moose; | ||||||||||
| 33 | use MooseX::Role::Defaultable; | ||||||||||
| 34 | |||||||||||
| 35 | has field1 => (is => 'rw'); | ||||||||||
| 36 | has field2 => (is => 'rw', default => 'blah'); | ||||||||||
| 37 | has field3 => (is => 'rw', default => sub { [] }); | ||||||||||
| 38 | |||||||||||
| 39 | __PACKAGE__->meta->make_immutable; | ||||||||||
| 40 | |||||||||||
| 41 | package main; | ||||||||||
| 42 | |||||||||||
| 43 | my $foo = Foo->new(); | ||||||||||
| 44 | print "Yes" if $foo->is_default; | ||||||||||
| 45 | $foo->field1('ignored'); | ||||||||||
| 46 | $foo->field2('active'); | ||||||||||
| 47 | $foo->field3(['complex']); | ||||||||||
| 48 | print "No" unless $foo->is_default; | ||||||||||
| 49 | print "Yes" if $foo->is_default('field1'); | ||||||||||
| 50 | $foo->restore_default; | ||||||||||
| 51 | print "Yes" if $foo->is_default; | ||||||||||
| 52 | |||||||||||
| 53 | =cut | ||||||||||
| 54 | |||||||||||
| 55 | =head1 METHODS | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | =head2 is_default | ||||||||||
| 60 | |||||||||||
| 61 | Return true if an object's attributes are set to their defaults. A list of | ||||||||||
| 62 | attribute names can be provided, restricting the check to only those | ||||||||||
| 63 | attributes. Unknown attributes and attributes with no defaults are both | ||||||||||
| 64 | considered to be in their default states. | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | 15 | ▶ | sub is_default { | ||||||||
|
Subroutine
is_default: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 69 | 15 | my $self = shift; | |||||||||
| 70 | |||||||||||
| 71 | my @attributes = @_ | ||||||||||
| 72 | 15 | ▶ | ? map { $self->meta->find_attribute_by_name($_) } @_ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | : $self->meta->get_all_attributes; | ||||||||||
| 74 | |||||||||||
| 75 | 15 | for my $attribute (grep { $_->has_default } @attributes) { | |||||||||
| 76 | 19 | ▶ | return 0 unless is_equal($attribute->default($self), $attribute->get_value($self)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 10 | return 1; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head2 restore_default | ||||||||||
| 83 | |||||||||||
| 84 | Set an object's attributes back to their defaults. This operation bypasses the | ||||||||||
| 85 | writers so triggers, coercions, and access restrictions will not apply. Any | ||||||||||
| 86 | attribute that does not have a default is ignored. A list of attribute names | ||||||||||
| 87 | can be provided, restricting the reset to only those attributes. | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||
| 90 | |||||||||||
| 91 | 3 | ▶ | sub restore_default { | ||||||||
|
Subroutine
restore_default: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 92 | 3 | my $self = shift; | |||||||||
| 93 | |||||||||||
| 94 | my @attributes = @_ | ||||||||||
| 95 | 3 | ▶ | ? grep { defined $_ } map { $self->meta->find_attribute_by_name($_) } @_ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | : $self->meta->get_all_attributes; | ||||||||||
| 97 | |||||||||||
| 98 | 3 | for my $attribute (grep { $_->has_default } @attributes) { | |||||||||
| 99 | 6 | $attribute->clear_value($self); | |||||||||
| 100 | 6 | $attribute->set_value($self, $attribute->default($self)); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 3 | return; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | =head1 AUTHOR | ||||||||||
| 107 | |||||||||||
| 108 | Aaron Cohen, C<< <aarondcohen at gmail.com> >> | ||||||||||
| 109 | |||||||||||
| 110 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 111 | |||||||||||
| 112 | This module was made possible by L<Shutterstock|http://www.shutterstock.com/> | ||||||||||
| 113 | (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source | ||||||||||
| 114 | projects from Shutterstock can be found at | ||||||||||
| 115 | L<code.shutterstock.com|http://code.shutterstock.com/>. | ||||||||||
| 116 | |||||||||||
| 117 | =head1 BUGS | ||||||||||
| 118 | |||||||||||
| 119 | Please report any bugs or feature requests to C<bug-MooseX-Role-Defaultable at rt.cpan.org>, or through | ||||||||||
| 120 | the web interface at L<https://github.com/aarondcohen/perl-moosex-role-defaultable/issues>. I will | ||||||||||
| 121 | be notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 SUPPORT | ||||||||||
| 124 | |||||||||||
| 125 | You can find documentation for this module with the perldoc command. | ||||||||||
| 126 | |||||||||||
| 127 | perldoc MooseX::Role::Defaultable | ||||||||||
| 128 | |||||||||||
| 129 | You can also look for information at: | ||||||||||
| 130 | |||||||||||
| 131 | =over 4 | ||||||||||
| 132 | |||||||||||
| 133 | =item * Official GitHub Repo | ||||||||||
| 134 | |||||||||||
| 135 | L<https://github.com/aarondcohen/perl-moosex-role-defaultable> | ||||||||||
| 136 | |||||||||||
| 137 | =item * GitHub's Issue Tracker (report bugs here) | ||||||||||
| 138 | |||||||||||
| 139 | L<https://github.com/aarondcohen/perl-moosex-role-defaultable/issues> | ||||||||||
| 140 | |||||||||||
| 141 | =item * CPAN Ratings | ||||||||||
| 142 | |||||||||||
| 143 | L<http://cpanratings.perl.org/d/MooseX-Role-Defaultable> | ||||||||||
| 144 | |||||||||||
| 145 | =item * Official CPAN Page | ||||||||||
| 146 | |||||||||||
| 147 | L<http://search.cpan.org/dist/MooseX-Role-Defaultable/> | ||||||||||
| 148 | |||||||||||
| 149 | =back | ||||||||||
| 150 | |||||||||||
| 151 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 152 | |||||||||||
| 153 | Copyright 2013 Aaron Cohen. | ||||||||||
| 154 | |||||||||||
| 155 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 156 | under the terms of either: the GNU General Public License as published | ||||||||||
| 157 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 158 | |||||||||||
| 159 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 160 | |||||||||||
| 161 | =cut | ||||||||||
| 162 | |||||||||||
| 163 | 1 | 1; # End of MooseX::Role::Defaultable | |||||||||