File Coverage

blib/lib/Data/RandomPerson.pm
Criterion Covered Total %
statement 91 97 93.8
branch 34 34 100.0
condition n/a
subroutine 21 21 100.0
pod 2 2 100.0
total 148 154 96.1


line stmt bran cond sub pod time code
1             package Data::RandomPerson;
2              
3 1     1   626 use strict;
  1         2  
  1         52  
4 1     1   7 use warnings;
  1         1  
  1         41  
5              
6 1     1   508 use Data::Random qw(rand_date);
  1         2619  
  1         62  
7 1     1   799 use Data::RandomPerson::Choice;
  1         2  
  1         548  
8              
9             our $VERSION = '0.50';
10              
11             sub new {
12 5     5 1 2577 my ( $class, %args ) = @_;
13              
14 5         15 my $self = bless {}, $class;
15              
16             ##
17             ## Set up the defaults for the various name classes
18             ## and see if they are being overwritten
19             ##
20              
21 5         6 my $male_class = 'Data::RandomPerson::Names::Male';
22 5         4 my $female_class = 'Data::RandomPerson::Names::Female';
23 5         6 my $last_class = 'Data::RandomPerson::Names::Last';
24              
25 5         12 foreach my $key ( keys %args ) {
26 7 100       24 if ( lc $key eq 'male' ) {
    100          
    100          
27 2         4 $male_class = $args{$key};
28 2         5 delete( $args{$key} );
29             }
30             elsif ( lc $key eq 'female' ) {
31 2         3 $female_class = $args{$key};
32 2         5 delete( $args{$key} );
33             }
34             elsif ( lc $key eq 'last' ) {
35 2         2 $last_class = $args{$key};
36 2         5 delete( $args{$key} );
37             }
38             else {
39 1         26 die "Unknown argument '$key' passed to new";
40             }
41             }
42              
43             ##
44             ## Now load the classes
45             ##
46              
47 1     1   198 eval "use $male_class;";
  0     1   0  
  0     1   0  
  1     1   447  
  1         3  
  1         37  
  1         5  
  1         1  
  1         10  
  1         4  
  1         1  
  1         12  
  4         236  
48 4 100       16 die "Unable to load '$male_class': $@" if $@;
49              
50 1     1   153 eval "use $female_class;";
  0     1   0  
  0     1   0  
  1         500  
  1         2  
  1         16  
  1         3  
  1         1  
  1         9  
  3         158  
51 3 100       14 die "Unable to load '$female_class': $@" if $@;
52              
53 1     1   151 eval "use $last_class;";
  0     1   0  
  0         0  
  1         2635  
  1         2  
  1         17  
  2         94  
54 2 100       14 die "Unable to load '$last_class': $@" if $@;
55              
56 1         6 $self->{mn} = $male_class->new();
57 1         7 $self->{fn} = $female_class->new();
58 1         6 $self->{ln} = $last_class->new();
59              
60 1         16 return $self;
61             }
62              
63             ################################################################################
64             # Subclass these if you want to change the gender, age and title calculated
65             ################################################################################
66              
67             sub _pick_gender {
68 20     20   47 my ($self) = @_;
69              
70 20 100       182 return ( rand() < 0.5 ) ? 'm' : 'f';
71             }
72              
73             sub _pick_age {
74 20     20   25 my ($self) = @_;
75              
76 20         61 return int( rand() * 100 ) + 1;
77             }
78              
79             sub _pick_title {
80 20     20   20 my ($self) = @_;
81              
82 20         62 my $choice = Data::RandomPerson::Choice->new();
83              
84 20 100       47 if ( $self->{gender} eq 'm' ) {
85 12         33 $choice->add( 'mr', 30 );
86 12 100       43 $choice->add('dr') if $self->{age} > 26;
87 12 100       38 $choice->add('prof') if $self->{age} > 34;
88 12 100       33 $choice->add('rev') if $self->{age} > 34;
89             }
90             else {
91 8         23 $choice->add('miss');
92 8 100       31 $choice->add('ms') if $self->{age} > 16;
93 8 100       40 $choice->add( 'mrs', 20 ) if $self->{age} > 16;
94 8 100       28 $choice->add('dr') if $self->{age} > 26;
95 8 100       24 $choice->add('prof') if $self->{age} > 34;
96 8 100       24 $choice->add('rev') if $self->{age} > 34;
97             }
98              
99 20         42 return ucfirst $choice->pick();
100             }
101              
102             ################################################################################
103             # These methods do not, typically, need to be overridden
104             ################################################################################
105              
106             sub _pick_lastname {
107 20     20   21 my ($self) = @_;
108              
109 20         84 return $self->{ln}->get();
110             }
111              
112             sub _pick_firstname {
113 20     20   23 my ($self) = @_;
114              
115 20 100       93 return ( $self->{gender} eq 'm' ) ? $self->{mn}->get() : $self->{fn}->get();
116             }
117              
118             sub _pick_dob {
119 20     20   24 my ($self) = @_;
120              
121 20         418 my $year = ( localtime() )[5] + 1900 - $self->{age};
122 20         116 my $dob = rand_date(min => "$year-01-01", max => "$year-12-31");
123              
124 20         30000 return $dob;
125             }
126              
127             sub create {
128 20     20 1 90319 my ($self) = @_;
129              
130             ## These settings have no prerequisites
131              
132 20         57 $self->{gender} = $self->_pick_gender();
133 20         48 $self->{age} = $self->_pick_age();
134 20         45 $self->{dob} = $self->_pick_dob();
135 20         56 $self->{lastname} = $self->_pick_lastname();
136              
137             ## This setting requires gender
138              
139 20         44 $self->{firstname} = $self->_pick_firstname();
140              
141             ## This setting requires gender and age
142              
143 20         43 $self->{title} = $self->_pick_title();
144              
145 20         128 return { dob => $self->{dob}, gender => $self->{gender}, age => $self->{age}, firstname => $self->{firstname}, lastname => $self->{lastname}, title => $self->{title} };
146             }
147              
148             1;
149              
150             __END__