File Coverage

blib/lib/Docker/Names/Random.pm
Criterion Covered Total %
statement 51 52 98.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 12 12 100.0
pod 2 2 100.0
total 70 73 95.8


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