File Coverage

blib/lib/ODS/Table/Generate/Data.pm
Criterion Covered Total %
statement 45 52 86.5
branch 1 2 50.0
condition n/a
subroutine 10 11 90.9
pod 0 6 0.0
total 56 71 78.8


line stmt bran cond sub pod time code
1             package ODS::Table::Generate::Data;
2 59     59   662747 use strict;
  59         118  
  59         1770  
3 59     59   236 use warnings;
  59         118  
  59         2655  
4 59     59   236 use YAOO;
  59         59  
  59         354  
5              
6             auto_build;
7              
8 59     59   20296 use ODS::Utils qw/load/;
  59         118  
  59         531  
9              
10 59     59   46079 use Term::ProgressSpinner;
  59         1313930  
  59         53985  
11              
12             has table_class => isa(string);
13              
14             has table_class_type => isa(string);
15              
16             has table_class_connect => isa(hash);
17              
18             has total => isa(integer);
19              
20             # Acme::MetaSyntactic class or one that follows the same standard
21             has data_class => isa(string('Acme::MetaSyntactic')), build_order(1);
22              
23             has data_class_theme => isa(string), build_order(2);
24              
25             has data_class_total => isa(integer(10));
26              
27             has auto_increment => isa(integer), default(0);
28              
29             has default_strings => isa(array), delay, coerce(sub {
30             my ($self, $data) = @_;
31              
32             if ($self->data_class_theme) {
33             load $self->data_class;
34             my $meta = $self->data_class->new($self->data_class_theme);
35             my @syntactic = $meta->name( $self->data_class_total );
36             return \@syntactic;
37             }
38             return [
39             "Barbarians",
40             "Gunga Din",
41             "Fame and Fortune",
42             "Anthem For Doomed Youth",
43             "You're My Waterloo",
44             "Belly of the Beast",
45             "Iceman",
46             "Heart of the Matter",
47             "The Milkman's Horse",
48             "Ruling Force",
49             "Worlds on Fire",
50             "Ninja",
51             "Open Eyes",
52             "Dollars and Dimes",
53             "Saturday",
54             "More Fire",
55             "Ghetto Long Time",
56             "Liberate",
57             "Today",
58             "Ave Adore",
59             "House I Built",
60             "Catch a Vibe",
61             "Chasing Cars",
62             "Gleaming Auction",
63             "Made of Stone",
64             "Bounce",
65             "Forest",
66             "Sweet Disposition",
67             "Science Of Fear",
68             "The Motto",
69             "Mysterious Ways"
70             ];
71             });
72              
73             has default_integers => isa(array([ 1 .. 100 ]));
74              
75             has default_floats => isa(array([ (map { sprintf "%.2f", "1.$_" } 0 .. 99) ]));
76              
77             has default_emails_domains => isa(array([
78             "lnation.org",
79             "world-wide.world",
80             "notifi.site",
81             "supervisor.fun",
82             "supervisor.guru",
83             "autonomous.cyou",
84             "notifi.page"
85             ]));
86              
87             has default_epoch => isa(array([ map { 1645016769 - (300 * $_) } 1 .. 50 ]));
88              
89             has default_phone => isa(array), delay, coerce(sub {
90             my ($self) = @_;
91             my @numbers = ();
92              
93             my $total = $self->data_class_total;
94              
95             for (1 .. $self->data_class_total) {
96             push @numbers, '+' . map { int(rand(10)) } 0 .. 12
97             }
98             return \@numbers;
99             });
100              
101             sub generate {
102 59     59 0 1475 my ($self) = @_;
103              
104 59         236 load $self->table_class;
105              
106 59         472 my $table = $self->table_class->connect($self->table_class_type, $self->table_class_connect);
107              
108 59         5900 $table->table->rows([]);
109              
110 59         2419 my $ps = Term::ProgressSpinner->new(
111             text_color => 'black on_bright_black',
112             precision => 2,
113             );
114              
115 59         92807 $ps->start($self->total);
116              
117 59         651301 my $i = 1;
118 59         590 while ($ps->advance) {
119 1711         2463073 my %row = ();
120 1711         2950 for my $column ( keys %{ $table->table->columns } ) {
  1711         7670  
121 25665         253877 $column = $table->table->columns->{$column};
122 25665         281902 my $callback = 'generate_' . $column->{type};
123 25665         43778 $row{$column->name} = $self->$callback($column);
124             }
125 1711         19057 $table->create(\%row);
126             }
127             }
128              
129             sub generate_integer {
130 1711     1711 0 4366 my ($self, $row) = @_;
131              
132 1711 50       4661 if ($row->auto_increment) {
133 1711         12626 $self->auto_increment($self->auto_increment + 1);
134 1711         52923 return $self->auto_increment;
135             }
136              
137 0         0 my $integers = $self->default_integers;
138              
139 0         0 return $integers->[int(rand(scalar @{$integers}))]
  0         0  
140             }
141              
142             sub generate_string {
143 17110     17110 0 21122 my ($self, $row) = @_;
144              
145 17110         24190 my $strings = $self->default_strings;
146              
147              
148 17110         66021 return $strings->[int(rand(scalar @{$strings}))];
  17110         42952  
149             }
150              
151             sub generate_boolean {
152 3422     3422 0 4071 my ($self, $row) = @_;
153              
154 3422         5310 my $boolean = int(rand(2));
155              
156 3422         7434 return \$boolean;
157             }
158              
159             sub generate_phone {
160 0     0 0 0 my ($self, $row) = @_;
161              
162 0         0 my $phone = $self->default_phone;
163              
164 0         0 return $phone->[int(rand(scalar @{$phone}))];
  0         0  
165             }
166              
167             sub generate_epoch {
168 3422     3422 0 5428 my ($self, $row) = @_;
169              
170 3422         7552 my $epoch = $self->default_epoch;
171              
172 3422         13924 return $epoch->[int(rand(scalar @{$epoch}))];
  3422         9912  
173             }
174              
175              
176             1;