Skip to content

Commit

Permalink
make reason read only
Browse files Browse the repository at this point in the history
  • Loading branch information
hannaeko committed Aug 25, 2021
1 parent 169019f commit b32c1b9
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions lib/Zonemaster/Backend/Errors.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ has '+code' => (

has 'reason' => (
isa => 'Str',
is => 'rw',
initializer => 'reason',
is => 'ro'
);

has 'method' => (
Expand All @@ -89,24 +88,16 @@ sub _build_method {
return $c[3];
}

around 'reason' => sub {
my $orig = shift;
my $self = shift;

my ( $value, $setter, $attr ) = @_;

# reader
return $self->$orig if not $value;
around 'BUILDARGS', sub {
my ($orig, $class, %args) = @_;

# trim new lines
$value =~ s/\n/ /g;
$value =~ s/^\s+|\s+$//g;

# initializer
return $setter->($value) if $setter;
if(exists $args{reason}) {
# trim new lines
$args{reason} =~ s/\n/ /g;
$args{reason} =~ s/^\s+|\s+$//g;
}

# writer
$self->$orig($value);
$class->$orig(%args);
};

sub as_string {
Expand Down

0 comments on commit b32c1b9

Please sign in to comment.