File: AttributeTags.pm

package info (click to toggle)
libmoosex-attributetags-perl 0.005-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: perl: 83; makefile: 2; sh: 1
file content (190 lines) | stat: -rw-r--r-- 4,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use 5.008;
use strict;
use warnings;

package MooseX::AttributeTags;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.005';

use Carp;
use Data::OptList qw(mkopt);
use Scalar::Util qw(blessed);

my $subname = eval { require Sub::Name; 'Sub::Name'->can('subname') }
	|| do { require Sub::Util; 'Sub::Util'->can('set_subname') };

my $yah = 1; # avoid exported subs becoming constants

sub import
{
	my $caller = caller;
	my $class  = shift;
	my $opts   = mkopt(\@_);
	my $prole  = $class->_prole;
	
	for (@$opts)
	{
		my ($traitname, $traitdesc) = @$_;
		$traitdesc ||= [];
		ref($traitdesc) eq 'ARRAY'
			or croak("Expected arrayref, not $traitdesc; stopped");
		
		my %attrs;
		my $inner_opts = mkopt($traitdesc);
		for (@$inner_opts)
		{
			my ($attrname, $attrdesc) = @$_;
			$attrs{$attrname} = $class->_canonicalize_attribute_spec($attrdesc);
		}
		
		my $traitqname = sprintf('%s::%s', $caller, $traitname);
		my $trait = $prole->generate_role(
			package    => $traitqname,
			parameters => { attributes => \%attrs },
		);
		
		my $coderef = $subname->($traitqname, sub () { $traitqname if $yah });
		no strict 'refs';
		*$traitqname = $coderef;
	}
}

sub _prole
{
	require MooseX::AttributeTags::PRole;
	Class::MOP::class_of('MooseX::AttributeTags::PRole');
}

sub _canonicalize_attribute_spec
{
	shift;
	my $spec = $_[0];
	
	return [ is => 'ro' ]
		unless defined $spec;
	
	return $spec
		if ref $spec eq 'ARRAY';
	
	return [ %$spec ]
		if ref $spec eq 'HASH';
	
	return [ is => 'ro', lazy => 1, default => $spec ]
		if ref $spec eq 'CODE';
	
	return [ is => 'ro', isa => $spec ]
		if blessed($spec) && $spec->isa('Moose::Meta::TypeConstraint');
	
	croak("Expected coderef/arrayref/hashref/constraint, not $spec; stopped");
}

1;

__END__

=pod

=encoding utf-8

=head1 NAME

MooseX::AttributeTags - tag your Moose attributes

=head1 SYNOPSIS

   package User;
   
   use Moose;
   use MooseX::Types::Moose 'Bool';
   use MooseX::AttributeTags (
      SerializationStyle => [
         hidden => Bool,
      ],
   );
   
   has username => (
      traits => [ SerializationStyle ],
      is     => 'ro',
      hidden => 0,
   );
   
   has password => (
      traits => [ SerializationStyle ],
      is     => 'rw',
      hidden => 1,
   );

=head1 DESCRIPTION

MooseX::AttributeTags is a factory for attribute traits. All the work is
done in the import method.

=head2 Methods

=over

=item C<< import(@optlist) >>

The option list is a list of trait names to create (which will be exported
to the caller package as constants).

Each trait name may be optionally followed by an arrayref of attributes to
be created within the trait. (In the SYNOPSIS, the "SerializationStyle" trait
gets an attribute called "hidden".)

Each attribute may be optionally followed by I<one> of:

=over

=item *

A coderef which provides a default value for the attribute.

=item *

A type constraint object (such as those provided by Types::Standard or
MooseX::Types; not a type constraint string) to validate the attribute.

=item *

An arrayref or hashref providing options similar to those given to
Moose's C<has> keyword.

=back

=back

Note that in the SYNOPSIS example, a constant C<< User::SerializationStyle >>
is defined.

   my $attr = User->meta->get_attribute('username');
   $attr->does(User::SerializationStyle);    # true
   $attr->hidden;                            # false

=head1 BUGS

Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=MooseX-AttributeTags>.

=head1 SEE ALSO

L<Moose>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013, 2017, 2019 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.