File Coverage

blib/lib/Art/World/Util.pm
Criterion Covered Total %
statement 131 164 79.8
branch n/a
condition 11 54 20.3
subroutine 24 28 85.7
pod 0 7 0.0
total 166 253 65.6


line stmt bran cond sub pod time code
1 11     11   12990 use 5.20.0;
  11         43  
2 11     11   63 use strict;
  11         24  
  11         263  
3 11     11   58 use warnings;
  11         22  
  11         672  
4              
5             package Art::World::Util {
6              
7             our $VERSION = '0.19_01';
8              
9 11     11   67 use Zydeco;
  11         24  
  11         84  
10              
11 11     11   349936 use feature qw( postderef );
  11         26  
  11         484  
12 11     11   61 no warnings qw( experimental::postderef );
  11         22  
  11         582  
13              
14 11     11   21686 class Math {
  11         29  
  11         100  
  11         249  
15             method pick ( Int $min, Int $max ) {
16 0   0 0 0 0 return $min + int ( rand ( $max - $min ));
  0   0     0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
17 0         0 }
18 11     11   636406 }
  11     11   26  
  11         194  
  11         167  
  11         5189  
  11         30  
  11         46  
19              
20 11     11   58519 class Meta {
  11         27  
  11         101  
  11         260  
21             method get_class( Object $klass ) {
22             return ref $klass;
23 17   33 17 0 167 }
  17   66     59  
  17         25  
  17         248  
  17         46  
  17         1557  
  17         165  
24 17         113  
  11         390  
25 11         197 method get_set_attributes_only( Object $clazz ) {
26             return keys %{ $clazz };
27 0   0 0 0 0 }
  0   0     0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
28 0         0  
  0         0  
  11         199  
29 11         168 method get_all_attributes( Object $claxx ) {
30             return keys( %{
31 0   0 0 0 0 'Moo'->_constructor_maker_for(
  0   0     0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
32             $self->get_class( $claxx )
33 0         0 )->all_attribute_specs
  0         0  
34             });
35             }
36 11         180  
37 11         130 method random_set_of_agents ( Int $num ) { }
38 11   0 11 0 1288426 }
  11   0 11   27  
  11     0   193  
  11         97  
  0         0  
  0         0  
  0         0  
  11         5135  
  11         26  
  11         49  
  0         0  
  0         0  
  0         0  
  0         0  
39              
40             # Makes possible to generate fake Persons very easily
41 11     11   85429 class Person {
  11         26  
  11         99  
42              
43 11         6700 require Faker;
  11         720645  
44 11   33     217 has firstname ( isa => Str ) = $self->generator->person_first_name;
  27         281  
  27         108  
  27         213  
  11         539  
45 11   33     129 has lastname ( isa => Str ) = $self->generator->person_last_name;
  27         428535  
  27         118  
  27         181  
  11         239  
46 11         148 has generator ( isa => InstanceOf[ 'Faker' ] ) = Faker->new;
  27         59666  
  11         5989  
47 11   33     157 has fake_name ( isa => Str ) = $self->firstname .' '. $self->lastname;
  27         2277  
  27         119  
  27         435  
48 11         240  
49             method generate_discourse( ArrayRef $buzz = [] ) {
50 1   33 1 0 6 for ( 0 .. int rand( 3 )) { push $buzz->@*, Faker->new->company_buzzword_type1 };
  1   33     5  
  1         3  
  1         124  
  1         18  
  1         1412  
  1         13  
51 1         9 return join ' ', $buzz->@*;
  3         30396  
52 1         954 }
53 11     11   3676755 }
  11     11   30  
  11         202  
  11         310  
  1         12  
  11         4973  
  11         23  
  11         48  
54              
55 11     11   28085 class String {
  11         31  
  11         81  
  11         257  
56             method titlify( Str $token ) {
57 1   33 1 0 5 return join '', map { ucfirst lc $_ } split /(\s+)/, $token;
  1   33     9  
  1         4  
  1         6  
  1         17  
  1         1231  
  1         17  
58 1         12 }
  5         18  
59 11     11   322580 }
  11     11   27  
  11         211  
  11         161  
  11         5060  
  11         28  
  11         47  
60              
61 11     11   45915 class Time {
  11         30  
  11         85  
62 11         6684 require Time::Moment;
63 11         13497 # YYYY-MM-DDThh:mm:ss
64             has source! (
65 11         134 isa =>
66             ( Str ) &
67 11         70848 ( StrMatch[ qr/ \A \d{4} - \d{2} - \d{2} T \d{2} : \d{2} : \d{2} \z /x ] ));
68 11   33     166 has datetime ( isa => InstanceOf[ 'Time::Moment' ], lazy => true ) = Time::Moment->from_string( $self->source . 'Z' );
  1         131516  
  1         5  
  1         56  
69 11     11   1497981 }
  11     11   31  
  11         195  
  11         6344  
  11         26  
  11         50  
70             }
71              
72             1;
73              
74             __END__
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             Art::World::Util - Generating all kind of data for the Art::World
81              
82             =head1 SYNOPSIS
83              
84             use Art::World::Util;
85              
86             say Art::World::Util->new_person->fake_name;
87             #==> Firtname Lastname
88              
89             say Art::World::Util->new_math->pick( 10, 1000 ));
90             #==> 666
91              
92             =head1 DESCRIPTION
93              
94             C<Art::World::Util> provide useful generation tools for automated C<Agents>
95             creation, data manipulation and other utilities that are not strictly related to
96             the Art::World entities.
97              
98             =head2 Methods
99              
100             =head3 Art::World::Math
101              
102             Artists usually don't like maths too much.
103              
104             =head4 pick()
105              
106             Pick an integer between a range that can be passed as a parameter. Mostly a way
107             to not have to memorize C<$min + int ( rand ( $max - $min ))>.
108              
109             =head3 Art::World::Meta
110              
111             Looks like the Art::World::Meta toolkit. See
112             L<https://metacpan.org/pod/Class::MOP::Class> for extreme cases.
113              
114             my $meta = $self->meta;
115              
116             Also there is the Zydeco's C<$class> object.
117              
118             This is a couple of utilities that makes a sort of meta-programming very simple. It is
119             more like a reminder for my bad memory than something very interesting. Largely
120             inspired by L<this Perl Monks thread|https://www.perlmonks.org/?node_id=1043195>.
121              
122             Art::World::Meta->get_all_attributes( $artist );
123             # ==> ( 'id', 'name', 'reputation', 'artworks', 'collectors', 'collected', 'status' )
124              
125             Mostly useless since Zydeco rely on Moo(se) so the Moose Meta Object Protocol is
126             available.
127              
128             =head4 get_class( Object $klass )
129              
130             Returns the class of the object.
131              
132             =head4 get_set_attributes_only( Object $clazz )
133              
134             Returns only attributes that are set for a particular object.
135              
136             =head4 get_all_attributes( Object $claxx )
137              
138             Returns even non-set attributes for a particular object.
139              
140             =head3 Art::World::Person
141              
142             =head4 fake_name()
143              
144             Generate a complete person name using L<Faker>.
145              
146             =head3 Art::World::String
147              
148             =head4 titlify()
149              
150             =head3 Art::World::Time
151              
152             Handy way for generating a L<Time::Moment> object that can be used for C<Events> for example.
153              
154             my $t = Art::World::Util->new_time( source => '2020-02-16T08:18:43' );
155              
156             =head1 AUTHOR
157              
158             Sébastien Feugère <sebastien@feugere.net>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             Copyright 2006-2021 Sebastien Feugère
163              
164             This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.