Debian Codegraph @

lib/Test2/Harness/Settings/Prefix.pm

raw
1 package Test2::Harness::Settings::Prefix;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Carp();
8 use Test2::Harness::Util();
9
10 sub new {
11 my $class = shift;
12 my $hash = {@_};
13 return bless \$hash, $class;
14 }
15
16 sub vivify_field {
17 my $self = shift;
18 my ($field) = @_;
19
20 return \(${$self}->{$field});
21 }
22
23 sub check_field {
24 my $self = shift;
25 my ($field) = @_;
26
27 return exists ${$self}->{$field};
28 }
29
30 sub field : lvalue {
31 my $self = shift;
32 my ($field, @args) = @_;
33
34 Carp::croak("Too many arguments for field()") if @args > 1;
35 Carp::croak("The '$field' field does not exist") unless exists ${$self}->{$field};
36
37 (${$self}->{$field}) = @args if @args;
38
39 return ${$self}->{$field};
40 }
41
42 sub remove_field {
43 my $self = shift;
44 my ($field) = @_;
45 delete ${$self}->{$field};
46 }
47
48 our $AUTOLOAD;
49 sub AUTOLOAD : lvalue {
50 my $this = shift;
51
52 my $field = $AUTOLOAD;
53 $field =~ s/^.*:://g;
54
55 return if $field eq 'DESTROY';
56
57 Carp::croak("Method $field() must be called on a blessed instance") unless ref($this);
58 Carp::croak("Too many arguments for $field()") if @_ > 1;
59
60 $this->field($field, @_);
61 }
62
63 sub TO_JSON {
64 my $self = shift;
65 return {%$$self};
66 }
67
68 sub build {
69 my $self = shift;
70 my ($class, @args) = @_;
71
72 require(Test2::Harness::Util::mod2file($class));
73
74 return $class->new(%$$self, @args);
75 }
76
77 1;
78
79 __END__
80
81 =pod
82
83 =encoding UTF-8
84
85 =head1 NAME
86
87 Test2::Harness::Settings::Prefix - Abstraction of a settings category, aka prefix.
88
89 =head1 DESCRIPTION
90
91 This class represents a settings category (prefix).
92
93 =head1 SYNOPSIS
94
95 # You will rarely if ever need to construct settings yourself, usually a
96 # component of Test2::Harness will expose them to you.
97 my $settings = $thing->settings;
98 my $display = $settings->display;
99
100 # Once you have your prefix you can read data from it:
101 my $verbose = $display->verbose;
102
103 # If you dislike autoload methods you can use the 'field' method:
104 my $verbose = $display->field('verbose');
105
106 # You can also change values:
107 $display->field(verbose => 1);
108
109 # You can also use the autoloaded method as an lvalue, but this breaks on
110 # perls older than 5.16, so it is not used internally, and you should only
111 # use it if you know you will never need an older perl:
112 $display->verbose = 1;
113
114 =head1 METHODS
115
116 Note that any field that does not conflict with the predefined methods can be
117 accessed via AUTOLOAD generating the methods as needed.
118
119 =over 4
120
121 =item $scalar_ref = $prefix->vivify_field($field_name)
122
123 This will force a field into existence. It returns a scalar reference to the
124 field which can be used to set the value:
125
126 my $vref = $display->vivify_field('verbose'); # Create or find field
127 ${$vref} = 1; # set verbosity to 1
128
129 =item $bool = $prefix->check_field($field_name)
130
131 Check if a field is defined or not.
132
133 =item $val = $prefix->field($field_name)
134
135 =item $val = $prefix->$field_name
136
137 =item $prefix->field($field_name, $val)
138
139 =item $prefix->$field_name = $val
140
141 Retrieve or set the value of the specified field. This will throw an exception
142 if the field does not exist.
143
144 B<Note>: The lvalue form C<< $prefix->$field_name = $val >> breaks on perls
145 older then 5.16.
146
147 =item $thing = $prefix->build($class, @args)
148
149 This will create an instance of C<$class> passing the key/value pairs from the
150 prefix as arguments. Additional arguments can be provided in C<@args>.
151
152 =item $hashref = $prefix->TO_JSON()
153
154 This method allows settings to be serialized into JSON.
155
156 =back
157
158 =head1 SOURCE
159
160 The source code repository for Test2-Harness can be found at
161 F<http://github.com/Test-More/Test2-Harness/>.
162
163 =head1 MAINTAINERS
164
165 =over 4
166
167 =item Chad Granum E<lt>[email protected]<gt>
168
169 =back
170
171 =head1 AUTHORS
172
173 =over 4
174
175 =item Chad Granum E<lt>[email protected]<gt>
176
177 =back
178
179 =head1 COPYRIGHT
180
181 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
182
183 This program is free software; you can redistribute it and/or
184 modify it under the same terms as Perl itself.
185
186 See F<http://dev.perl.org/licenses/>
187
188 =cut