Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move segment towards being stricter about presence versus data, add t…

…ests
  • Loading branch information...
commit c75f0d9f70a2c4f23534db68ffe6d5f82e04ddeb 1 parent 8404f3b
@sorear authored
Showing with 6 additions and 15 deletions.
  1. +5 −4 lib/X12/Schema/Segment.pm
  2. +1 −11 lib/X12/Schema/SegmentUse.pm
View
9 lib/X12/Schema/Segment.pm
@@ -12,7 +12,7 @@ has incomplete => (isa => 'Bool', is => 'ro', default => 0);
sub encode {
my ($self, $sink, $obj) = @_;
- die 'Segment '.$self->name." must be encoded using a HASH\n" unless $obj && ref($obj) eq 'HASH' && !blessed($obj);
+ die 'Segment '.$self->friendly." must be encoded using a HASH\n" unless $obj && ref($obj) eq 'HASH' && !blessed($obj);
$_->check($obj) for @{ $self->constraints };
@@ -33,11 +33,12 @@ sub encode {
}
}
- die "Excess fields for segment ".$self->name.": ".join(', ', sort keys %tmp) if %tmp;
+ die "Excess fields for segment ".$self->friendly.": ".join(', ', sort keys %tmp) if %tmp;
pop @bits while @bits && $bits[-1] eq '';
- $sink->segment( join($sink->element_sep, $self->tag, @bits) . $sink->segment_term ) if @bits;
- return @bits ? 1 : 0;
+ die "Segment ".$self->friendly." must contain data if it is present" unless @bits;
+
+ $sink->segment( join($sink->element_sep, $self->tag, @bits) . $sink->segment_term );
}
__PACKAGE__->meta->make_immutable;
View
12 lib/X12/Schema/SegmentUse.pm
@@ -5,23 +5,13 @@ use namespace::autoclean;
with 'X12::Schema::Sequencable';
-has def => (is => 'ro', isa => 'X12::Schema::Segment', required => 1);
-
-sub encode {
- my ($self, $sink, $obj) = @_;
-
- if (!$self->def->encode($sink, $obj) && $self->required) {
- die 'Segment '.$self->name." must contain data\n";
- }
-}
+has def => (is => 'ro', isa => 'X12::Schema::Segment', required => 1, handles => ['encode']);
sub BUILD {
my ($self) = @_;
# DIVERSITY: possibly worth restricting use of 'B' type here?
- # we can only be empty in the event that we are optional, but in that case
- # Sequence will automatically handle _can_be_empty and _ambiguous_end_tags.
$self->_can_be_empty(0);
$self->_ambiguous_end_tags({});
$self->_initial_tags({ $self->def->tag => 1 });
Please sign in to comment.
Something went wrong with that request. Please try again.