File Coverage

blib/lib/Docker/Names/Random.pm
Criterion Covered Total %
statement 47 48 97.9
branch 3 4 75.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 2 2 100.0
total 63 67 94.0


line stmt bran cond sub pod time code
1             package Docker::Names::Random;
2              
3             ## no critic (Documentation::RequirePodAtEnd)
4             ## no critic (Documentation::RequirePodSections)
5              
6 3     3   245898 use strict;
  3         13  
  3         88  
7 3     3   17 use warnings;
  3         7  
  3         73  
8 3     3   14 use utf8; # The __DATA__ is UTF-8
  3         6  
  3         14  
9 3     3   1761 use English qw(-no_match_vars);
  3         7165  
  3         19  
10              
11             # ABSTRACT: Create random strings like Docker does for container names.
12              
13             our $VERSION = '0.0.1'; # VERSION: generated by DZP::OurPkgVersion
14              
15 3     3   1353 use Exporter 'import';
  3         6  
  3         197  
16             our @EXPORT_OK = qw( docker_name );
17             our %EXPORT_TAGS = ( 'all' => [qw( docker_name )] );
18              
19 3     3   1633 use YAML::PP;
  3         159869  
  3         168  
20 3     3   1751 use Hash::Util qw( lock_keys );
  3         9409  
  3         22  
21              
22              
23             # Internal functions
24              
25             sub _get_data {
26 3     3   20 my $ypp = YAML::PP->new;
27 3         11084 local $INPUT_RECORD_SEPARATOR = undef;
28 3         547 my $local_data = ;
29 3         22 my ($data) = $ypp->load_string($local_data);
30 3         2619622 return $data;
31             }
32              
33             sub new {
34 2     2 1 1821 my $class = shift;
35              
36 2         13 my $data = _get_data();
37 2         17 my $self = { data => $data };
38 2         22 bless $self, $class;
39 2         6 lock_keys( %{$self} );
  2         915  
40 2         48 return $self;
41             }
42              
43              
44             sub docker_name {
45 2     2 1 5617 my $self = shift;
46 2         5 my $data;
47 2 100       10 if ( defined $self ) {
48 1         3 $data = $self->{'data'};
49             }
50             else {
51              
52             # We are not running as an object method
53 1         7 $data = _get_data();
54             }
55              
56 2         10 my ( $adj, $surname );
57              
58             CREATE_NAME:
59 2         492 my $r_for_adj = int( rand( scalar @{ $data->{'adjectives'} } ) );
  2         21  
60 2         23 $adj = $data->{'adjectives'}->[$r_for_adj]->{'word'};
61 2         7 my $r_for_name = int( rand( scalar @{ $data->{'names'} } ) );
  2         7  
62 2         9 $surname = $data->{'names'}->[$r_for_name]->{'surname'};
63              
64 2 50 33     13 if ( $adj eq 'boring' && $surname eq 'wozniak' ) {
65              
66             # Steve Wozniak is not boring
67 0         0 goto 'CREATE_NAME';
68             }
69              
70 2         194 return $adj . q{_} . $surname;
71             }
72              
73              
74             1;
75              
76             =pod
77              
78             =encoding UTF-8
79              
80             =head1 NAME
81              
82             Docker::Names::Random - Create random strings like Docker does for container names.
83              
84             =head1 VERSION
85              
86             version 0.0.1
87              
88             =head1 SYNOPSIS
89              
90             # As an object (if you create many, this is more efficient).
91             require Docker::Names::Random;
92              
93             my $dnr = Docker::Names::Random->new();
94             my $random_name1 = $dnr->docker_name();
95              
96             # As an imported function.
97             use Docker::Names::Random qw( docker_name );
98             # OR
99             use Docker::Names::Random qw( :all );
100              
101             my $random_name2 = docker_name();
102              
103             =head1 DESCRIPTION
104              
105             If you are using Docker, you may have noticed that it
106             creates random names for containers when you haven't provided any
107             specific name. These names are a combination of an adjective
108             and a proper name of an individual. The individuals are
109             famous men and women picked from the history of scientific exploration
110             and engineering.
111              
112             This package allows you to use the same system in your own programs.
113             You would get combinations like
114             I,
115             I,
116             I,
117             I,
118             I and
119             I.
120              
121             The combination I is not allowed because
122             L is not boring.
123             This same limitation exists in the
124             L.
125              
126             =for stopwords overthruster Docker::Names::Random Docker Names Random
127              
128             =head1 STATUS
129              
130             This module is currently being developed so changes in the API are possible,
131             though unlikely.
132              
133             =head1 METHODS
134              
135             =head2 new
136              
137             Create object.
138              
139             require Docker::Names::Random;
140             my $dnr = Docker::Names::Random->new();
141              
142             =head2 docker_name
143              
144             Return a random string.
145              
146             my $random_name = $dnr->docker_name();
147              
148             =head1 FUNCTIONS
149              
150             =head2 docker_name
151              
152             Return a random string.
153              
154             use Docker::Names::Random qw( docker_name );
155             # OR
156             use Docker::Names::Random qw( :all );
157              
158             my $random_name = docker_name();
159              
160             =head1 AUTHOR
161              
162             Mikko Johannes Koivunalho
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             This software is copyright (c) 2020 by Mikko Johannes Koivunalho.
167              
168             This is free software; you can redistribute it and/or modify it under
169             the same terms as the Perl 5 programming language system itself.
170              
171             =cut
172              
173             __DATA__