File Coverage

blib/lib/KiokuX/User/ID.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 14 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package KiokuX::User::ID;
4 2     2   1279 use MooseX::Role::Parameterized;
  2         7  
  2         17  
5              
6 2     2   20348 use namespace::clean -except => 'meta';
  2         5  
  2         25  
7              
8             parameter id_attribute => (
9             isa => 'Str',
10             default => 'id',
11             );
12              
13             parameter user_prefix => (
14             isa => 'Str',
15             default => 'user:',
16             );
17              
18             role {
19             my ($p) = @_;
20             my $id_attr = $p->id_attribute;
21             my $user_prefix = $p->user_prefix;
22              
23             with qw(KiokuDB::Role::ID);
24              
25             method id_for_user => sub {
26 2     2   9 my ( $self, $id ) = @_;
27 2         14 return $user_prefix . $id;
28             };
29              
30             method kiokudb_object_id => sub {
31 2     2   5 my $self = shift;
32 2         70 $self->id_for_user($self->$id_attr);
33             };
34              
35             has $id_attr => (
36             isa => "Str",
37             is => "ro",
38             required => 1,
39             );
40             };
41              
42             __PACKAGE__
43              
44             __END__
45              
46             =pod
47              
48             =head1 NAME
49              
50             KiokuX::User::ID - L<KiokuDB::Role::ID> integration for user objects
51              
52             =head1 SYNOPSIS
53              
54             with qw(KiokuX::User::ID);
55              
56             =head1 DESCRIPTION
57              
58             This role provides an C<id> attribute for user objects, and self registers in
59             the L<KiokuDB> directory with the object ID C<user:$user_id>.
60              
61             Using this role implies that user IDs are immutable.
62              
63             =head1 METHODS
64              
65             =over 4
66              
67             =item kiokudb_object_id
68              
69             Implements the required method from L<KiokuX::User::ID> by prefixing the C<id>
70             attribute with C<user:>.
71              
72             =item id_for_user $username
73              
74             Mangles the username into an ID by prefixing the string C<user:>.
75              
76             Can be overriden to provide custom namespacing.
77              
78             Can also be used as a class method from the model:
79              
80             sub get_identity_by_username {
81             my ( $self, $username ) = @_;
82              
83             my $object_id = MyFoo::Schema::Identity::Username->id_for_user($username);
84              
85             return $self->lookup($object_id);
86             }
87              
88             =back
89              
90             =head1 ATTRIBUTES
91              
92             =over 4
93              
94             =item id
95              
96             This is the user's ID in the system. It is not the object ID, but the object ID
97             is derived from it.
98              
99             =back
100              
101             =cut
102              
103             # ex: set sw=4 et:
104