blib/lib/DBIx/Class/RandomStringColumns.pm

filter: Statementstmt 100% 42 / 42 Branchbran 80.0% 8 / 10 Conditioncond 72.7% 8 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 87.5% 70 / 80 CC 12 SCAR 15.2
CC
12
Cov
92%
random_string_columns
7.1
insert
5.0
get_random_string
2.0
CRAP 4.6
↑ Summary Build »
1 package DBIx::Class::RandomStringColumns;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.09';
6  
7 4 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use String::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $LENGTH = 32;
12 4 our $SALT = '[A-Za-z0-9]';
13  
14 4 __PACKAGE__->mk_classdata( 'rs_definition' );
15  
16 sub random_string_columns {
17 9 ▶ my $self = shift;
Subroutine random_string_columns: called CC=7 SCAR=19.6
Pod documented
18  
19 9 my $opt = pop @_;
20 9 ▶ if ( ref $opt ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt ne "HASH") 7 2 9
21 7 push @_, $opt;
22 7 $opt = {};
23 }
24  
25 9 ▶ my $length = $opt->{length} || $LENGTH;
Condition66%2/3
$opt->{'length'} || $DBIx::Class::RandomStringColumns::LENGTH
l!l&&r!l&&!r
270
MC/DC0%0/2
$opt->{'length'} || $DBIx::Class::RandomStringColumns::LENGTH
$opt->{'length'} $DBIx::Class::RandomStringColumns::LENGTH
Truth table66%2/3
A || B
A= $opt->{'length'}B= $DBIx::Class::RandomStringColumns::LENGTH
ABresult
1X1
011
000
26 9 ▶ my $salt = $opt->{salt} || $SALT;
Condition66%2/3
$opt->{'salt'} || $DBIx::Class::RandomStringColumns::SALT
l!l&&r!l&&!r
270
MC/DC0%0/2
$opt->{'salt'} || $DBIx::Class::RandomStringColumns::SALT
$opt->{'salt'} $DBIx::Class::RandomStringColumns::SALT
Truth table66%2/3
A || B
A= $opt->{'salt'}B= $DBIx::Class::RandomStringColumns::SALT
ABresult
1X1
011
000
27  
28 9 ▶ my $rs_definition = $self->rs_definition || {};
Condition100%2/2
$self->rs_definition || {}
l!l
54
MC/DC100%1/1
$self->rs_definition || {}
$self->rs_definition
Truth table100%2/2
A
A= $self->rs_definition
Aresult
11
00
29 9 for my $column (@_) {
30 11 ▶ $self->throw_exception("column $column doesn't exist")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_column($column) 0 11 11
31 unless $self->has_column($column);
32 11 $rs_definition->{$column}->{length} = $length;
33 11 $rs_definition->{$column}->{salt} = $salt;
34 }
35  
36 9 $self->rs_definition( $rs_definition );
37 }
38  
39 sub insert {
40 4 ▶ my $self = shift;
Subroutine insert: called CC=5 SCAR=16.1
Pod documented
41 4 ▶ for my $column ( keys( %{ $self->rs_definition || {} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->rs_definition or {} 1 3 4
42 10 ▶ $self->store_column( $column, $self->get_random_string($column) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_column($column) and not $self->get_column($column) 7 3 10
Condition66%2/3
$self->has_column($column) and not $self->get_column($column)
!ll&&!rl&&r
037
MC/DC50%1/2
$self->has_column($column) and not $self->get_column($column)
$self->has_column($column) not $self->get_column($column)
Truth table66%2/3
A and not(B)
A= $self->has_column($column)B= not $self->get_column($column)
ABresult
0X0
100
111
43 if $self->has_column($column) && !$self->get_column($column);
44 }
45 4 $self->next::method(@_);
46 }
47  
48 sub get_random_string {
49 7 ▶ my $self = shift;
Subroutine get_random_string: called CC=2 SCAR=7.0
Pod documented
50 7 my $column = shift;
51  
52 7 my $rs =
53 $self->result_source->schema->resultset(
54 $self->result_source->source_name );
55 7 my $val;
56 7 ▶ do { # must be unique
Branch: 1 branch
BranchTrueFalseTotal
$rs->search({"me.$column", $val})->count and do { do { $val = "String::Random"->new->randregex(sprintf("%s{%d}", $self->rs_definition->{$column}{"salt"}, $self->rs_definition->{$column}{"length"})) } } 0 7 7
57 $val = String::Random->new->randregex(
58 sprintf( '%s{%d}',
59 $self->rs_definition->{$column}->{salt},
60 $self->rs_definition->{$column}->{length},
61 )
62 7 );
63 } while ( $rs->search( { "me.$column" => $val } )->count );
64  
65 7 return $val;
66 }
67  
68 4 1;
69  
70 __END__
71  
72 =head1 NAME
73  
74 DBIx::Class::RandomStringColumns - Implicit random string columns
75  
76 =head1 SYNOPSIS
77  
78 pacakge CD;
79 __PACKAGE__->load_components(qw/RandomStringColumns Core DB/);
80 __PACKAGE__->random_string_columns('uid');
81  
82 pacakge Artist;
83 __PACKAGE__->load_components(qw/RandomStringColumns Core DB/);
84 __PACKAGE__->random_string_columns(['rid', {length => 10}]);
85  
86 package LoginUser
87 __PACKAGE__->load_components(qw/RandomStringColumns Core DB/);
88 __PACKAGE__->random_string_columns(
89 ['rid', {length => 10}],
90 ['login_id', {length => 15, solt => '[0-9]'}],
91 );
92  
93 =head1 DESCRIPTION
94  
95 This L<DBIx::Class> component reassemble the behavior of
96 L<Class::DBI::Plugin::RandomStringColumn>, to make some columns implicitly created as random string.
97  
98 Note that the component needs to be loaded before Core.
99  
100 =head1 METHODS
101  
102 =head2 insert
103  
104 =head2 random_string_columns
105  
106 $pkg->random_string_columns('uid'); # uid column set random string.
107 $pkg->random_string_columns(['rid', {length=>10}]); # set string length.
108 # set multi column rule
109 $pkg->random_string_columns(
110 'uid',
111 ['rid', {length => 10}],
112 ['login_id', {length => 15, solt => '[0-9]'}],
113 );
114  
115 this method need column name, and random string generate option.
116 option is "length", and "solt".
117  
118 =head2 get_random_string
119  
120 =head1 AUTHOR
121  
122 Kan Fushihara C<< <kan __at__ mobilefactory.jp> >>
123  
124 =head1 LICENSE AND COPYRIGHT
125  
126 Copyright (c) 2006, Kan Fushihara C<< <kan __at__ mobilefactory.jp> >>. All rights reserved.
127  
128 This module is free software; you can redistribute it and/or
129 modify it under the same terms as Perl itself. See L<perlartistic>.
130  
↑ Summary Build »