File Coverage

blib/lib/xDT/RecordType.pm
Criterion Covered Total %
statement 3 5 60.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 5 7 71.4


line stmt bran cond sub pod time code
1             package xDT::RecordType;
2              
3 1     1   1046 use v5.10;
  1         3  
4 1     1   147 use Moose;
  0            
  0            
5             use namespace::autoclean;
6             use Carp;
7             use XML::Simple;
8             use File::Basename;
9              
10             =head1 NAME
11              
12             xDT::RecordType - The record type of a xDT record.
13              
14             =head1 VERSION
15              
16             Version 1.00
17              
18             =cut
19              
20             our $VERSION = '1.00';
21              
22              
23             =head1 SYNOPSIS
24              
25             Quick summary of what the module does.
26              
27             Perhaps a little code snippet.
28              
29             use xDT::RecordType;
30              
31             my $foo = xDT::RecordType->new();
32             ...
33              
34             =head1 EXPORT
35              
36             A list of functions that can be exported. You can delete this section
37             if you don't export anything, such as for a purely object-oriented module.
38              
39             =head1 CONSTANTS
40              
41             =head2 LENGTH
42             The maximum length of a record type identifier.
43             =cut
44              
45             use constant {
46             LENGTH => 4,
47              
48             };
49              
50             =head1 ATTRIBUTES
51              
52             =head2 id
53             Unique identifier of this record type.
54             =cut
55              
56             has id => (
57             is => 'ro',
58             isa => 'Str',
59             required => 1,
60             reader => 'getId',
61             trigger => \&_checkId,
62             documentation => 'Unique identifier of this record type.',
63             );
64              
65             =head2 labels
66             The human readable labels of this record type. Language is used as key value.
67             =cut
68              
69             has labels => (
70             is => 'ro',
71             isa => 'Maybe[HashRef[Str]]',
72             reader => 'getLabels',
73             documentation => 'The human readable labels of this record type. Language is used as key value.',
74             );
75              
76             =head2 accessor
77             Short string for easy access to this record via xDT::Object.
78             =cut
79              
80             has accessor => (
81             is => 'ro',
82             isa => 'Str',
83             required => 1,
84             reader => 'getAccessor',
85             documentation => 'Short string for easy access to this record via xDT::Object.',
86             );
87              
88             =head2 length
89             Max length of this record type.
90             =cut
91              
92             has length => (
93             is => 'ro',
94             isa => 'Maybe[Str]',
95             reader => 'getLength',
96             documentation => 'Max length of this record type.',
97             );
98              
99             =head2 type
100             Corresponds to xDT record type string.
101             =cut
102              
103             has type => (
104             is => 'ro',
105             isa => 'Maybe[Str]',
106             reader => 'getType',
107             documentation => 'Corresponds to xDT record type string.'
108             );
109              
110             around BUILDARGS => sub {
111             my $orig = shift;
112             my $class = shift;
113              
114             if (@_ == 1 && !ref $_[0]) {
115             return $class->$orig(_extractParametersFromConfigFile($_[0]));
116             } else {
117             my %params = @_;
118             return $class->$orig(_extractParametersFromConfigFile($params{'id'}));
119             }
120             };
121              
122             =head1 SUBROUTINES/METHODS
123              
124             =head2 isObjectEnd
125             Checks if this record type is an ending record
126             =cut
127              
128             sub isObjectEnd {
129             my $self = shift;
130              
131             return $self->getId == 8201;
132             }
133              
134             =head2 getId
135             Returns the id of this record type.
136             =cut
137              
138             =head2 getLabels
139             Returns the labels of this record type.
140             =cut
141              
142             =head2 getAccessor
143             Returns the accessor of this record type.
144             =cut
145              
146             =head2 getLength
147             Returns the maximum length of this recourd type.
148             =cut
149              
150             =head2 getType
151             Returns the type of this record type.
152             =cut
153              
154             sub _extractParametersFromConfigFile {
155             my $id = shift // croak('Error: parameter $id missing.');
156              
157             my $xml = new XML::Simple(
158             KeyAttr => { RecordType => 'id', label => 'lang' },
159             ForceArray => 1,
160             ContentKey => '-content',
161             );
162             my $config = $xml->XMLin(File::Basename::dirname(__FILE__). '/Configuration/RecordTypes.xml')
163             ->{RecordType}->{$id};
164            
165             return (
166             id => $id,
167             labels => $config->{label},
168             type => $config->{type},
169             accessor => $config->{accessor} // $id,
170             length => $config->{length},
171             );
172             }
173              
174              
175             sub _checkId {
176             my ($self, $id) = @_;
177              
178             croak(sprintf("Error: attribute 'id' has length %d (should be %d).", length $id, LENGTH))
179             unless (length $id == LENGTH);
180             }
181              
182             =head1 AUTHOR
183              
184             Christoph Beger, C<< >>
185              
186             =head1 BUGS
187              
188             Please report any bugs or feature requests to C, or through
189             the web interface at L. I will be notified, and then you'll
190             automatically be notified of progress on your bug as I make changes.
191              
192              
193              
194              
195             =head1 SUPPORT
196              
197             You can find documentation for this module with the perldoc command.
198              
199             perldoc xDT::RecordType
200              
201              
202             You can also look for information at:
203              
204             =over 4
205              
206             =item * RT: CPAN's request tracker (report bugs here)
207              
208             L
209              
210             =item * AnnoCPAN: Annotated CPAN documentation
211              
212             L
213              
214             =item * CPAN Ratings
215              
216             L
217              
218             =item * Search CPAN
219              
220             L
221              
222             =back
223              
224              
225             =head1 ACKNOWLEDGEMENTS
226              
227              
228             =head1 LICENSE AND COPYRIGHT
229              
230             Copyright 2017 Christoph Beger.
231              
232             This program is released under the following license: MIT
233              
234              
235             =cut
236              
237             __PACKAGE__->meta->make_immutable;
238              
239             1; # End of xDT::RecordType