initial code import
[gitmo/MooseX-SemiAffordanceAccessor.git] / lib / MooseX / Policy / SemiAffordanceAccessor.pm
CommitLineData
a338e27f 1package MooseX::Policy::SemiAffordanceAccessor;
2
3use strict;
4use warnings;
5
6our $VERSION = '0.01';
7our $AUTHORITY = 'cpan:DROLSKY';
8
9use constant attribute_metaclass => 'MooseX::Policy::SemiAffordanceAccessor::Attribute';
10
11
12package MooseX::Policy::SemiAffordanceAccessor::Attribute;
13
14use Moose;
15
16extends 'Moose::Meta::Attribute';
17
18before '_process_options' => sub
19{
20 my $class = shift;
21 my $name = shift;
22 my $options = shift;
23
24 if ( exists $options->{is} &&
25 ! ( exists $options->{reader} || exists $options->{writer} ) )
26 {
27 if ( $options->{is} eq 'ro' )
28 {
29 $options->{reader} = $name;
30 }
31 elsif ( $options->{is} eq 'rw' )
32 {
33 $options->{reader} = $name;
34
35 my $prefix = 'set';
36 if ( $name =~ s/^_// )
37 {
38 $prefix = '_set';
39 }
40
41 $options->{writer} = $prefix . q{_} . $name;
42 }
43
44 delete $options->{is};
45 }
46};
47
48
491;
50
51__END__
52
53=pod
54
55=head1 NAME
56
57MooseX::Policy::SemiAffordanceAccessor - A policy to name accessors foo() and set_foo()
58
59=head1 SYNOPSIS
60
61 use Moose::Policy 'MooseX::Policy::SemiAffordanceAccessor';
62 use Moose;
63
64 # make some attributes
65
66=head1 DESCRIPTION
67
68This class does not provide any methods. Just loading it changes the
69default naming policy for the package so that accessors are separated
70into get and set methods. The get methods have the same name as the
71accessor, while set methods are prefixed with "set_".
72
73If you define an attribute with a leading underscore, then the set
74method will start with "_set_".
75
76The name "semi-affordance" comes from David Wheeler's Class::Meta
77module.
78
79=head1 AUTHOR
80
81Dave Rolsky, C<< <autarch@urth.org> >>
82
83=head1 BUGS
84
85Please report any bugs or feature requests to
86C<bug-moosex-policy-semiaffordanceaccessor@rt.cpan.org>, or through
87the web interface at L<http://rt.cpan.org>. I will be notified, and
88then you'll automatically be notified of progress on your bug as I
89make changes.
90
91=head1 COPYRIGHT & LICENSE
92
93Copyright 2007 Dave Rolsky, All Rights Reserved.
94
95This program is free software; you can redistribute it and/or modify
96it under the same terms as Perl itself.
97
98=cut