Aion-Enum

 view release on metacpan or  search on metacpan

lib/Aion/Enum.pm  view on Meta::CPAN


=head1 NAME

Aion :: Enum - Listing in the style of OOP, when each renewal is an object

=head1 VERSION

0.0.3

=head1 SYNOPSIS

File lib/StatusEnum.pm:

	package StatusEnum;
	
	use Aion::Enum;
	
	# Active status
	case active => 1, 'Active';
	
	# Passive status
	case passive => 2, 'Passive';
	
	1;



	use StatusEnum;
	
	&StatusEnum::active->does('Aion::Enum') # => 1
	
	StatusEnum->active->name   # => active
	StatusEnum->passive->value # => 2
	StatusEnum->active->alias  # => Active status
	StatusEnum->passive->stash # => Passive
	
	[ StatusEnum->cases   ] # --> [StatusEnum->active, StatusEnum->passive]
	[ StatusEnum->names   ] # --> [qw/active passive/]
	[ StatusEnum->values  ] # --> [qw/1 2/]
	[ StatusEnum->aliases ] # --> ['Active status', 'Passive status']
	[ StatusEnum->stashes ] # --> [qw/Active Passive/]

=head1 DESCRIPTION

C<Aion :: Enum> allows you to create transfers-objects. These transfers may contain additional methods and properties. You can add roles to them (using C<with>) or use them as a role.

An important feature is the preservation of the procedure for listing.

C<Aion::Enum> is similar to php8 enums, but has the additional properties C<alias> and C<stash>.

=head1 SUBROUTINES

=head2 case ($name, [$value, [$stash]])

Creates a listing: his constant.

	package OrderEnum {
	    use Aion::Enum;
	
	    case 'first';
	    case second => 2;
	    case other  => 3, {data => 123};
	}
	
	&OrderEnum::first->name  # => first
	&OrderEnum::first->value # -> undef
	&OrderEnum::first->stash # -> undef
	
	&OrderEnum::second->name  # => second
	&OrderEnum::second->value # -> 2
	&OrderEnum::second->stash # -> undef
	
	&OrderEnum::other->name  # => other
	&OrderEnum::other->value # -> 3
	&OrderEnum::other->stash # --> {data => 123}

=head2 issa ($nameisa, [$valueisa], [$stashisa], [$aliasisa])

Indicates the type (ISA) of meanings and additions.

Its name is a reference to the goddess Isse from the story “Under the Moles of Mars” Burrose.

	eval {
	package StringEnum;
	    use Aion::Enum;
	
	    issa Str => Int => Undef => Undef;
	
	    case active => "Active";
	};
	$@ # ~> active value must have the type Int. The it is 'Active'
	
	eval {
	package StringEnum;
	    use Aion::Enum;
	
	    issa Str => Str => Int;
	
	    case active => "Active", "Passive";
	};
	$@ # ~> active stash must have the type Int. The it is 'Passive'

File lib/StringEnum.pm:

	package StringEnum;
	use Aion::Enum;
	
	issa Str => Undef => Undef => StrMatch[qr/^[A-Z]/];
	
	# pushkin
	case active => ;
	
	1;



	require StringEnum # @-> active alias must have the type StrMatch[qr/^[A-Z]/]. The it is 'pushkin'!

=head1 CLASS METHODS

=head2 cases ($cls)

List of transfers.

	[ OrderEnum->cases ] # --> [OrderEnum->first, OrderEnum->second, OrderEnum->other]

=head2 names ($cls)

Names of transfers.

	[ OrderEnum->names ] # --> [qw/first second other/]

=head2 values ($cls)

The values of the transfers.

	[ OrderEnum->values ] # --> [undef, 2, 3]

=head2 stashes ($cls)

Additions of transfers.

	[ OrderEnum->stashes ] # --> [undef, undef, {data => 123}]

=head2 aliases ($cls)

Pseudonyms of transfers.

LIB/authorenum.pm file:

	package AuthorEnum;
	
	use Aion::Enum;
	
	# Pushkin Aleksandr Sergeevich
	case pushkin =>;
	
	# Yacheykin Uriy
	case yacheykin =>;
	
	case nouname =>;
	
	1;



	require AuthorEnum;
	[ AuthorEnum->aliases ] # --> ['Pushkin Aleksandr Sergeevich', 'Yacheykin Uriy', undef]

=head2 fromName ($cls, $name)

Get Case by name with exceptions.

	OrderEnum->fromName('first') # -> OrderEnum->first
	eval { OrderEnum->fromName('not_exists') }; $@ # ~> Did not case with name `not_exists`!

=head2 tryFromName ($cls, $name)

Get Case by name.

	OrderEnum->tryFromName('first')      # -> OrderEnum->first
	OrderEnum->tryFromName('not_exists') # -> undef

=head2 fromValue ($cls, $value)

Get Case by value with exceptions.

	OrderEnum->fromValue(undef) # -> OrderEnum->first
	eval { OrderEnum->fromValue('not-exists') }; $@ # ~> Did not case with value `not-exists`!

=head2 tryFromValue ($cls, $value)



( run in 1.702 second using v1.01-cache-2.11-cpan-5a3173703d6 )