blib/lib/Net/EGTS/SubRecord/Auth/DispatcherIdentity.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 95 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use utf8;
2  
3 package Net::EGTS::SubRecord::Auth::DispatcherIdentity;
4 use Mouse;
5 extends qw(Net::EGTS::SubRecord);
6  
7 use Carp;
8 use List::MoreUtils qw(natatime);
9 use Encode qw();
10  
11 use Net::EGTS::Util qw(usize dumper_bitstring);
12 use Net::EGTS::Codes;
13  
14 # Dispatcher Type
15 has DT => is => 'rw', isa => 'BYTE', default => 0;
16 # Dispatcher ID
17 has DID => is => 'rw', isa => 'UINT';
18 # Description
19 has DSCR =>
20 is => 'rw',
21 isa => 'Maybe[STRING]',
22 trigger => sub {
23 my ($self, $value, $old) = @_;
24 use bytes;
25 die 'Description too long' if defined($value) && length($value) > 255;
26 }
27 ;
28  
29 after 'decode' => sub {
30 my ($self) = @_;
31 die 'SubRecord not EGTS_SR_DISPATCHER_IDENTITY type'
32 unless $self->SRT == EGTS_SR_DISPATCHER_IDENTITY;
33  
34 my $bin = $self->SRD;
35 $self->DT( $self->nip(\$bin => 'C') );
36 $self->DID( $self->nip(\$bin => 'L') );
37 $self->DSCR( $self->nip(\$bin => 'a*' => length($bin)) );
38 };
39  
40 before 'encode' => sub {
41 my ($self) = @_;
42 die 'SubRecord not EGTS_SR_DISPATCHER_IDENTITY type'
43 unless $self->SRT == EGTS_SR_DISPATCHER_IDENTITY;
44  
45 my $bin = pack 'C L' => $self->DT, $self->DID;
46 $bin .= pack 'a*' => $self->DSCR if defined $self->DSCR;
47  
48 $self->SRD( $bin );
49 };
50  
51 around BUILDARGS => sub {
52 my $orig = shift;
53 my $class = shift;
54  
55 # simple scalar decoding support
56 my $bin = @_ % 2 ? shift : undef;
57 my %opts = @_;
58  
59 # Description is CP-1251
60 $opts{DSCR} = Encode::encode('CP1251', $opts{DSCR}) if defined $opts{DSCR};
61  
62 return $class->$orig( bin => $bin, %opts, SRT => EGTS_SR_DISPATCHER_IDENTITY)
63 if $bin;
64 return $class->$orig( %opts, SRT => EGTS_SR_DISPATCHER_IDENTITY);
65 };
66  
67 augment as_debug => sub {
68 my ($self) = @_;
69 use bytes;
70  
71 my @bytes = ((unpack('B*', $self->SRD)) =~ m{.{8}}g);
72  
73 my @str;
74 push @str => sprintf('DT: %s', splice @bytes, 0 => usize('C'));
75 push @str => sprintf('DID: %s %s %s %s', splice @bytes, 0 => usize('L'));
76  
77 my $it = natatime 4, @bytes;
78 my @chunks;
79 while (my @vals = $it->()) {
80 push @chunks, join(' ', @vals);
81 }
82 push @str => sprintf('DSCR: %s', join("\n ", @chunks));
83  
84 return @str;
85 };
86  
87 __PACKAGE__->meta->make_immutable();