1 package DBIx::Class::TokenGroup;
5 use base qw( DBIx::Class );
9 DBIx::Class::TokenGroup - Search for tokens in a tree of groups. (EXPERIMENTAL)
13 Define your user tokens class.
15 package Schema::User::Token;
16 use base qw( DBIx::Class::Core );
17 __PACKAGE__->table('user_tokens');
18 __PACKAGE__->add_columns(qw( name user_id value ));
19 __PACKAGE__->set_primary_key(qw( name user_id ));
22 Define your group tokens class.
24 package Schema::Group::Token;
25 use base qw( DBIx::Class::Core );
26 __PACKAGE__->table('group_tokens');
27 __PACKAGE__->add_columns(qw( name group_id value ));
28 __PACKAGE__->set_primary_key(qw( name group_id ));
31 Define your group class.
33 package Schema::Group;
34 use base qw( DBIx::Class::Core );
35 __PACKAGE__->load_components(qw(
38 __PACKAGE__->table('groups');
39 __PACKAGE__->add_columns(qw( group_id parent_id ));
40 __PACKAGE__->set_primary_key('group_id');
41 __PACKAGE__->parent_column('parent_id');
42 __PACKAGE__->has_many( 'tokens' => 'Group::Token' => 'group_id' );
45 Define your user class.
48 use base qw( DBIx::Class::Core );
49 __PACKAGE__->table('users');
50 __PACKAGE__->add_columns(qw( user_id group_id ));
51 __PACKAGE__->set_primary_key('user_id');
52 __PACKAGE__->token_name_column('name');
53 __PACKAGE__->token_value_column('value');
54 __PACKAGE__->has_many( 'tokens' => 'User::Token' => 'user_id' );
55 __PACKAGE__->belongs_to( 'group' => 'Group', { 'foreign.group_id' => 'self.group_id' } );
60 This L<DBIx::Class> component provides several utilities for
61 retrieving tokens for a tree of groups. A token is, at a minimum,
62 a name and a value. Groups are associated using either
63 L<DBIx::Class::Tree::AdjacencyList> or L<DBIx::Class::Tree::NestedSet>.
65 This component itself is fairly simple, but it requires that you
66 structure your classes in a certain way.
70 The sample shown in the SYNOPSIS is just that, an example.
71 As long your clases respond the way that this component
72 expects it doesn't care how things are structure. So, here
73 are the requirements for the class that uses this component.
79 A tokens() method that returns a DBIx::Class::ResultSet object. The
80 objects (tokens) that the result set returns must have the name and
81 value columns that you specified with the...
91 =head2 token_name_column
93 __PACKAGE__->token_name_column('name');
95 Sets the name of the column that can be queried to
96 retrieve a token's name.
100 __PACKAGE__->mk_classdata( 'token_name_column' => 'name' );
102 =head2 token_value_column
104 __PACKAGE__->token_value_column('value');
106 Sets the name of the column that can be queried to
107 retrieve a token's value. This settings is optional
108 as long as you do not use the token_true() and
109 token_false() methods.
113 __PACKAGE__->mk_classdata( 'token_value_column' => 'value' );
117 $object->token('name');
119 Returns the token object, or 0 if none was found.
124 my( $self, $name ) = @_;
125 my $name_col = $self->token_name_column();
126 my $token = $self->tokens->search({
129 return $token if ($token);
130 $token = $self->group->tokens->search({
133 return $token if ($token);
134 my $descendant = $self->group->descendant_by_depth();
135 while (my $group = $descendant->next()) {
136 $token = $group->tokens->search({
139 return $token if ($token);
146 if ($object->token_exists('name')){ ... }
148 Tests whether there is a token defined of the
154 my( $self, $name ) = @_;
155 my $name_col = $self->token_name_column();
156 return 1 if( $self->tokens->search({
159 return 1 if( $self->group->tokens->search({
162 my $ancestors = $self->group->ancestors_by_depth();
163 while (my $group = $ancestors->next()) {
164 return 1 if( $group->tokens->search({
173 if ($object->token_true('name')) {
175 Returns 1 if the token exists and its value is a
176 true value. Returns 0 otherwise.
181 my( $self, $name ) = @_;
182 my $token = $self->token( $name );
183 return 0 if(!$token);
184 return ( $token->get_column($self->token_value_column()) ? 1 : 0 );
189 if ($object->token_false('name')) {
191 Returns 1 if the token exists and its value is a
192 false value. Returns 0 otherwise.
197 my( $self, $name ) = @_;
198 my $token = $self->token( $name );
199 return 0 if(!$token);
200 return ( $token->get_column($self->token_value_column()) ? 0 : 1 );
208 Aran Clary Deltac <bluefeet@cpan.org>
212 You may distribute this code under the same terms as Perl itself.