NAME

Aion::Meta::FeatureConstruct - accessor, predicate, initializer and clearer

SYNOPSIS

use Aion::Meta::FeatureConstruct;

our $construct = Aion::Meta::FeatureConstruct->new('My::Package', 'my_feature');

$construct->add_attr(':lvalue');

$construct->accessor # -> << 'END'
package My::Package {
	sub my_feature:lvalue {
		if (@_>1) {
			my ($self, $val) = @_;
			$self->{my_feature} = $val;
			$self
		} else {
			my ($self) = @_;
			$self->{my_feature}
		}
	}
}
END

DESCRIPTION

Designed for constructing getters/setters from pieces of code.

SUBROUTINES

new ($pkg, $name)

Constructor.

pkg

The package to which the attribute belongs. Getter.

$::construct->pkg # -> "My::Package"

name

Attribute name. Getter.

$::construct->name # -> "my_feature"

write

Code for writing the value. Getter.

$::construct->write # \> %(preset)s%(set)s%(trigger)s

read

Code to read the value. Getter.

$::construct->read # \> %(access)s%(getvar)s%(release)s%(ret)s

getvar

Variable to receive the value. Getter.

$::construct->getvar # \> %(get)s

ret

Value return code. Getter.

$::construct->ret # -> ''

init_arg

The key is in the initialization hash. Accessor.

$::construct->init_arg # \> %(name)s

set

Code for setting the value to the object hash. Accessor.

$::construct->set # \> $self->{%(name)s} = $val;

get

Code for getting a value from an object hash. Accessor.

$::construct->get # \> $self->{%(name)s}

has

Code for checking the existence of a value. Accessor.

$::construct->has # \> exists $self->{%(name)s}

clear

Code for deleting a value. Accessor.

$::construct->clear # \> delete $self->{%(name)s}

weaken

Link weakening code. Accessor.

$::construct->weaken # \> Scalar::Util::weaken(%(get)s);

accessor_name

The name of the accessor method. Accessor.

$::construct->accessor_name # \> %(name)s

reader_name

Reader method name. Accessor.

$::construct->reader_name # \> _get_%(name)s

writer_name

Writer method name. Accessor.

$::construct->writer_name # \> _set_%(name)s

predicate_name

Predicate method name. Accessor.

$::construct->predicate_name # \> has_%(name)s

clearer_name

The name of the cleanser method. Accessor.

$::construct->clearer_name # \> clear_%(name)s

initer

Attribute initialization code. Accessor.

$::construct->initer # \> %(initvar)s%(write)s

not_specified

Initialization code if no value is specified. Accessor.

$::construct->not_specified # -> ''

getter

Getter code in the accessor. Accessor.

$::construct->getter # \> %(read)s

setter

Setter code in the accessor. Default: '%(write)s'.

$::construct->setter # \> %(write)s

selfret

Return code from setter. Accessor.

$::construct->selfret # \> $self

add_attr($code, $unshift)

Adds an attribute to the accessor.

$::construct->add_attr(':bvalue');
$::construct->{attr} # --> [':lvalue', ':bvalue']
$::construct->add_attr(':a_value', 1);
$::construct->{attr} # --> [':a_value', ':lvalue', ':bvalue']

add_preset($code, $unshift)

Adds a preset code before recording.

$::construct->add_preset('die if $val < 0;', 1);
$::construct->{preset} # -> 'die if $val < 0;'

add_trigger($code, $unshift)

Adds a trigger after recording.

$::construct->add_trigger('$self->on_change;');
$::construct->{trigger} # -> '$self->on_change;'

add_cleaner($code, $unshift)

Adds cleanup code before deletion.

$::construct->add_cleaner('$self->{old} = $self->{attr};');
$::construct->{cleaner} # -> '$self->{old} = $self->{attr};'

add_access($code, $unshift)

Adds code to the getter before reading the attribute.

$::construct->add_access('die unless $self->{attr};');
$::construct->{access} # -> 'die unless $self->{attr};'

add_release($code, $unshift)

Adds code to the getter after reading.

$::construct->add_release('$val = undef;');
$::construct->{release} # -> '$val = undef;'

initializer

Generates code to initialize a feature in the constructor (new).

$::construct->initializer # -> << 'END'
		if (exists $value{my_feature}) {
			my $val = delete $value{my_feature};
			die if $val < 0;
			$self->{my_feature} = $val;
			$self->on_change;
		}
END

destroyer

Generates code for the destructor.

$::construct->destroyer # -> <<'END'
		if (exists $self->{my_feature}) {
			eval {
				$self->{old} = $self->{attr};
			};
			warn $@ if $@;
		}
END

accessor

Generates an accessor code.

$::construct->accessor # -> <<'END'
package My::Package {
	sub my_feature:a_value:lvalue:bvalue {
		if (@_>1) {
			my ($self, $val) = @_;
			die if $val < 0;
			$self->{my_feature} = $val;
			$self->on_change;
			$self
		} else {
			my ($self) = @_;
			die unless $self->{attr};
			my $val = $self->{my_feature};
			$val = undef;
			$val
		}
	}
}
END

reader

Generates getter code.

$::construct->reader # -> <<'END'
package My::Package {
	sub _get_my_feature {
		my ($self) = @_;
		die unless $self->{attr};
		my $val = $self->{my_feature};
		$val = undef;
		$val
	}
}
END

writer

Generates setter code.

$::construct->writer # -> <<'END'
package My::Package {
	sub _set_my_feature {
		my ($self, $val) = @_;
		die if $val < 0;
		$self->{my_feature} = $val;
		$self->on_change;
		$self
	}
}
END

predicate

Generates a predicate code.

$::construct->predicate # -> <<'END'
package My::Package {
	sub has_my_feature {
		my ($self) = @_;
		exists $self->{my_feature}
	}
}
END

clearer

Generates a purifier code.

$::construct->clearer # -> <<'END'
package My::Package {
	sub clear_my_feature {
		my ($self) = @_;
		if (exists $self->{my_feature}) {
			$self->{old} = $self->{attr};
			delete $self->{my_feature}
		}
		$self
	}
}
END

AUTHOR

Yaroslav O. Kosmina mailto:dart@cpan.org

LICENSE

GPLv3

COPYRIGHT

The Aion::Meta::FeatureConstruct module is copyright © 2025 Yaroslav O. Kosmina. Rusland. All rights reserved.