File Coverage

blib/lib/App/HL7/Compare/Parser/MessageConfig.pm
Criterion Covered Total %
statement 35 37 94.5
branch 3 6 50.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 47 53 88.6


line stmt bran cond sub pod time code
1             package App::HL7::Compare::Parser::MessageConfig;
2             $App::HL7::Compare::Parser::MessageConfig::VERSION = '0.004';
3 4     4   48 use v5.10;
  4         15  
4 4     4   21 use strict;
  4         9  
  4         85  
5 4     4   16 use warnings;
  4         14  
  4         185  
6              
7 4     4   20 use Moo;
  4         7  
  4         23  
8 4     4   1714 use Mooish::AttributeBuilder -standard;
  4         8  
  4         57  
9 4     4   2550 use Types::Common::String qw(StrLength);
  4         150960  
  4         36  
10 4     4   4943 use Carp qw(carp croak);
  4         10  
  4         352  
11              
12 4     4   24 use constant DEFAULT_SEPARATORS => '|^~\&';
  4         9  
  4         1971  
13              
14             has param 'segment_separator' => (
15             isa => StrLength [1, 2],
16             writer => 1,
17             default => sub { "\n" },
18             );
19              
20             has option 'field_separator' => (
21             isa => StrLength [1, 1],
22             writer => 1,
23             );
24              
25             has option 'component_separator' => (
26             isa => StrLength [1, 1],
27             writer => 1,
28             );
29              
30             has option 'repetition_separator' => (
31             isa => StrLength [1, 1],
32             writer => 1,
33             );
34              
35             has option 'escape_character' => (
36             isa => StrLength [1, 1],
37             writer => 1,
38             );
39              
40             has option 'subcomponent_separator' => (
41             isa => StrLength [1, 1],
42             writer => 1,
43             );
44              
45             sub from_MSH
46             {
47 14     14 0 33 my ($self, $input) = @_;
48 14         169 my $has_msh = $input =~ s/\AMSH//;
49              
50 14 50       44 if (!$has_msh) {
51 0         0 $input = DEFAULT_SEPARATORS;
52 0         0 carp "no MSH segment found - using default separators ($input)";
53             }
54              
55 14         64 my @order = qw(
56             field_separator
57             component_separator
58             repetition_separator
59             escape_character
60             subcomponent_separator
61             );
62              
63 14 50       57 croak 'Not enough input to read message control characters'
64             unless length $input >= @order;
65              
66 14         61 my @characters = split //, substr $input, 0, scalar @order;
67 14         35 foreach my $field (@order) {
68 70         1595 my $setter = "set_$field";
69 70         101 my $predicate = "has_$field";
70              
71 70         118 my $character = shift @characters;
72 70 50       230 next if $self->$predicate;
73 70         1414 $self->$setter($character);
74             }
75             }
76              
77             1;
78