File Coverage

blib/lib/Art/World/Util.pm
Criterion Covered Total %
statement 139 164 84.7
branch n/a
condition 14 54 25.9
subroutine 25 28 89.2
pod 0 7 0.0
total 178 253 70.3


line stmt bran cond sub pod time code
1 12     12   14043 use 5.20.0;
  12         47  
2 12     12   80 use strict;
  12         43  
  12         284  
3 12     12   60 use warnings;
  12         28  
  12         505  
4              
5             package Art::World::Util {
6              
7 12     12   76 use Zydeco;
  12         27  
  12         100  
8              
9 12     12   405803 use feature qw( postderef );
  12         66  
  12         501  
10 12     12   74 no warnings qw( experimental::postderef );
  12         44  
  12         586  
11              
12 12     12   25348 class Math {
  12         39  
  12         103  
  12         304  
13             method pick ( Int $min, Int $max ) {
14 100   33 100 0 269 return $min + int ( rand ( $max - $min ));
  100   66     250  
  100         161  
  100         114430  
  100         271  
  100         1694  
  100         2488  
15 100         2185 }
16 12     12   743967 }
  12     12   35  
  12         221  
  12         185  
  12         6027  
  12         36  
  12         56  
17              
18 12     12   68842 class Meta {
  12         30  
  12         90  
  12         304  
19             method get_class( Object $klass ) {
20             return ref $klass;
21 17   33 17 0 37 }
  17   66     52  
  17         27  
  17         257  
  17         56  
  17         1784  
  17         197  
22 17         118  
  12         414  
23 12         225 method get_set_attributes_only( Object $clazz ) {
24             return keys %{ $clazz };
25 0   0 0 0 0 }
  0   0     0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
26 0         0  
  0         0  
  12         235  
27 12         181 method get_all_attributes( Object $claxx ) {
28             return keys( %{
29 0   0 0 0 0 'Moo'->_constructor_maker_for(
  0   0     0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
30             $self->get_class( $claxx )
31 0         0 )->all_attribute_specs
  0         0  
32             });
33             }
34 12         217  
35 12         138 method random_set_of_agents ( Int $num ) { }
36 12   0 12 0 1506481 }
  12   0 12   36  
  12     0   233  
  12         118  
  0         0  
  0         0  
  0         0  
  12         6110  
  12         31  
  12         63  
  0         0  
  0         0  
  0         0  
  0         0  
37              
38             # Makes possible to generate fake Persons very easily
39 12     12   101709 class Person {
  12         36  
  12         95  
40              
41 12         8290 require Faker;
  12         817449  
42 12   33     229 has firstname ( isa => Str ) = $self->generator->person_first_name;
  127         1165  
  127         363  
  127         642  
  12         610  
43 12   33     144 has lastname ( isa => Str ) = $self->generator->person_last_name;
  127         640083  
  127         434  
  127         649  
  12         292  
44 12         164 has generator ( isa => InstanceOf[ 'Faker' ] ) = Faker->new;
  127         105387  
  12         6789  
45 12   33     165 has fake_name ( isa => Str ) = $self->firstname .' '. $self->lastname;
  127         9822  
  127         427  
  127         2158  
46 12         300  
47             method generate_discourse( ArrayRef $buzz = [] ) {
48 1   33 1 0 4 for ( 0 .. int rand( 3 )) { push $buzz->@*, Faker->new->company_buzzword_type1 };
  1   33     6  
  1         2  
  1         103  
  1         16  
  1         1145  
  1         12  
49 1         160 return join ' ', $buzz->@*;
  3         25382  
50 1         956 }
51 12     12   4481981 }
  12     12   38  
  12         268  
  12         311  
  1         11  
  12         5815  
  12         27  
  12         56  
52              
53 12     12   34265 class String {
  12         34  
  12         93  
  12         297  
54             method titlify( Str $token ) {
55 1   33 1 0 4 return join '', map { ucfirst lc $_ } split /(\s+)/, $token;
  1   33     9  
  1         118  
  1         5  
  1         16  
  1         867  
  1         16  
56 1         14 }
  5         35  
57 12     12   390914 }
  12     12   34  
  12         192  
  12         164  
  12         5574  
  12         31  
  12         59  
58              
59 12     12   54843 class Time {
  12         33  
  12         101  
60 12         8258 require Time::Moment;
61 12         16095 # YYYY-MM-DDThh:mm:ss
62             has source! (
63 12         151 isa =>
64             ( Str ) &
65 12         84254 ( StrMatch[ qr/ \A \d{4} - \d{2} - \d{2} T \d{2} : \d{2} : \d{2} \z /x ] ));
66 12   33     213 has datetime ( isa => InstanceOf[ 'Time::Moment' ], lazy => true ) = Time::Moment->from_string( $self->source . 'Z' );
  1         140686  
  1         8  
  1         67  
67 12     12   1867954 }
  12     12   43  
  12         251  
  12         7396  
  12         34  
  12         58  
68             }
69              
70             1;
71              
72             __END__
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Art::World::Util - Generating all kind of data for the Art::World
79              
80             =head1 SYNOPSIS
81              
82             use Art::World::Util;
83              
84             say Art::World::Util->new_person->fake_name;
85             #==> Firtname Lastname
86              
87             say Art::World::Util->new_math->pick( 10, 1000 ));
88             #==> 666
89              
90             =head1 DESCRIPTION
91              
92             C<Art::World::Util> provide useful generation tools for automated C<Agents>
93             creation, data manipulation and other utilities that are not strictly related to
94             the Art::World entities.
95              
96             =head2 Methods
97              
98             =head3 Art::World::Math
99              
100             Artists usually don't like maths too much.
101              
102             =head4 pick()
103              
104             Pick an integer between a range that can be passed as a parameter. Mostly a way
105             to not have to memorize C<$min + int ( rand ( $max - $min ))>.
106              
107             =head3 Art::World::Meta
108              
109             Looks like the Art::World::Meta toolkit. See
110             L<https://metacpan.org/pod/Class::MOP::Class> for extreme cases.
111              
112             my $meta = $self->meta;
113              
114             Also there is the Zydeco's C<$class> object.
115              
116             This is a couple of utilities that makes a sort of meta-programming very simple. It is
117             more like a reminder for my bad memory than something very interesting. Largely
118             inspired by L<this Perl Monks thread|https://www.perlmonks.org/?node_id=1043195>.
119              
120             Art::World::Meta->get_all_attributes( $artist );
121             # ==> ( 'id', 'name', 'reputation', 'artworks', 'collectors', 'collected', 'status' )
122              
123             Mostly useless since Zydeco rely on Moo(se) so the Moose Meta Object Protocol is
124             available.
125              
126             =head4 get_class( Object $klass )
127              
128             Returns the class of the object.
129              
130             =head4 get_set_attributes_only( Object $clazz )
131              
132             Returns only attributes that are set for a particular object.
133              
134             =head4 get_all_attributes( Object $claxx )
135              
136             Returns even non-set attributes for a particular object.
137              
138             =head3 Art::World::Person
139              
140             =head4 fake_name()
141              
142             Generate a complete person name using L<Faker>.
143              
144             =head3 Art::World::String
145              
146             =head4 titlify()
147              
148             =head3 Art::World::Time
149              
150             Handy way for generating a L<Time::Moment> object that can be used for C<Events> for example.
151              
152             my $t = Art::World::Util->new_time( source => '2020-02-16T08:18:43' );
153              
154             =head1 AUTHOR
155              
156             Sébastien Feugère <sebastien@feugere.net>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             Copyright 2006-2021 Sebastien Feugère
161              
162             This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.