Debian Codegraph @

lib/Test2/Harness/Settings.pm

raw
1 package Test2::Harness::Settings;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Carp();
8 use Scalar::Util();
9
10 use Test2::Harness::Settings::Prefix;
11
12 sub new {
13 my $class = shift;
14
15 my $hash;
16 if (@_ == 1) {
17 require Test2::Harness::Util::File::JSON;
18 my $settings_file = Test2::Harness::Util::File::JSON->new(name => $_[0]);
19 $hash = $settings_file->read;
20 }
21 else {
22 $hash = {@_};
23 }
24
25 for my $key (keys %$hash) {
26 my $val = delete $hash->{$key};
27
28 if (Scalar::Util::blessed($val)) {
29 Carp::croak("All prefixes must contain instances of Test2::Harness::Settings::Prefix")
30 unless $val->isa('Test2::Harness::Settings::Prefix');
31
32 $hash->{$key} = $val;
33 next;
34 }
35
36 Carp::croak("All prefixes must be defined as hashes")
37 unless ref($val) eq 'HASH';
38
39 $hash->{$key} = Test2::Harness::Settings::Prefix->new(%$val);
40 }
41
42 return bless(\$hash, $class);
43 }
44
45 sub define_prefix {
46 my $self = shift;
47 my ($prefix) = @_;
48
49 return ${$self}->{$prefix} //= Test2::Harness::Settings::Prefix->new;
50 }
51
52 sub check_prefix {
53 my $self = shift;
54 my ($prefix) = @_;
55 return exists(${$self}->{$prefix});
56 }
57
58 sub prefix {
59 my $self = shift;
60 my ($prefix, @args) = @_;
61
62 Carp::croak("Too many arguments for prefix()") if @args;
63 Carp::croak("The '$prefix' prefix is not defined") unless ${$self}->{$prefix};
64
65 return ${$self}->{$prefix};
66 }
67
68 sub build {
69 my $self = shift;
70 my ($prefix, $class, @args) = @_;
71
72 my $p = $self->prefix($prefix);
73
74 $p->build($class, @args);
75 }
76
77 our $AUTOLOAD;
78 sub AUTOLOAD {
79 my $this = shift;
80
81 my $prefix = $AUTOLOAD;
82 $prefix =~ s/^.*:://g;
83
84 return if $prefix eq 'DESTROY';
85
86 Carp::croak("Method $prefix() must be called on a blessed instance") unless ref($this);
87 Carp::croak("Too many arguments for $prefix()") if @_;
88
89 $this->prefix($prefix);
90 }
91
92 sub TO_JSON {
93 my $self = shift;
94 return {%$$self};
95 }
96
97 1;
98
99 __END__
100
101 =pod
102
103 =encoding UTF-8
104
105 =head1 NAME
106
107 Test2::Harness::Settings - Configuration settings for Test2::Harness.
108
109 =head1 DESCRIPTION
110
111 This module represents the options provided at the command line. Each option
112 has a prefix, and each prefix can be accessed from the settings.
113
114 =head1 SYNOPSIS
115
116 # You will rarely if ever need to construct settings yourself, usually a
117 # component of Test2::Harness will expose them to you.
118 my $settings = $thing->settings;
119
120 # All prefixes have a method generated for them via AUTOLOAD
121 my $display = $settings->display;
122
123 # You can also use the prefix method
124 my $display = $settings->prefix('display');
125
126
127 # The prefix can be used in a similar way
128 my $verbose = $settings->display->verbose;
129
130 See L<Test2::Harness::Settings::Prefix> for more details on how to use the prefixes.
131
132 =head1 METHODS
133
134 Note that any prefix that does not conflict with the predefined methods can be
135 accessed via AUTOLOAD generating the methods as needed.
136
137 =over 4
138
139 =item $settings->define_prefix($prefix_name)
140
141 This is used to create a prefix.
142
143 =item $bool = $settings->check_prefix($prefix_name)
144
145 This is used to check if a prefix is defined or not.
146
147 =item $prefix = $settings->prefix($prefix_name)
148
149 =item $prefix = $settings->$prefix_name
150
151 This will retrieve a prefix if it exists. If the prefix is not defined this
152 will throw an exception. If you are unsure if a prefix exists use
153 C<$settings->check_prefix($prefix_name)>.
154
155 =item $thing = $settings->build($prefix_name, $class, @args)
156
157 This will create an instance of C<$class> passing the key/value pairs from the
158 specified prefix as arguments. Additional arguments can be provided in
159 C<@args>.
160
161 =item $hashref = $settings->TO_JSON()
162
163 This method allows settings to be serialized into JSON.
164
165 =back
166
167 =head1 SOURCE
168
169 The source code repository for Test2-Harness can be found at
170 F<http://github.com/Test-More/Test2-Harness/>.
171
172 =head1 MAINTAINERS
173
174 =over 4
175
176 =item Chad Granum E<lt>[email protected]<gt>
177
178 =back
179
180 =head1 AUTHORS
181
182 =over 4
183
184 =item Chad Granum E<lt>[email protected]<gt>
185
186 =back
187
188 =head1 COPYRIGHT
189
190 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
191
192 This program is free software; you can redistribute it and/or
193 modify it under the same terms as Perl itself.
194
195 See F<http://dev.perl.org/licenses/>
196
197 =cut