my $name = $attribute->name;
my $key = $name;
my $default = $attribute->default;
- my $trigger = $attribute->trigger;
my $type = $attribute->type_constraint;
my $constraint = $attribute->find_type_constraint;
my $builder = $attribute->builder;
+ my $trigger = $attribute->trigger;
+ my $before = $trigger->{before};
+ my $after = $trigger->{after};
+ my $around = $trigger->{around};
+
my $accessor = 'sub {
my $self = shift;';
$accessor .= 'if (@_) {
local $_ = $_[0];';
- if ($constraint) {
- $accessor .= 'do {
- my $display = defined($_) ? overload::StrVal($_) : "undef";
- Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
- };'
+ if ($before) {
+ $accessor .= '$before->($self, $_, $attribute);';
+ }
+
+ if ($around) {
+ $accessor .= '$around->(sub {
+ my $self = shift;
+ $_ = $_[0];
+ ';
}
- $accessor .= '$self->{$key} = $_;';
+ if ($constraint) {
+ $accessor .= 'do {
+ my $display = defined($_) ? overload::StrVal($_) : "undef";
+ Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
+ };'
+ }
+
+ $accessor .= '$self->{$key} = $_;';
+
+ if ($attribute->is_weak_ref) {
+ $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
+ }
- if ($attribute->is_weak_ref) {
- $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
+ if ($around) {
+ $accessor .= '}, $self, $_, $attribute);';
}
- if ($trigger) {
- $accessor .= '$trigger->($self, $_, $attribute);';
+ if ($after) {
+ $accessor .= '$after->($self, $_, $attribute);';
}
$accessor .= '}';