| 1 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package Data::Conveyor::Value::Ticket::Stage; | ||
| 6 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | $Data::Conveyor::Value::Ticket::Stage::VERSION = '1.103130'; | |
| 8 | } | ||
| 9 | # ABSTRACT: Stage-based conveyor-belt-like ticket handling system | ||
| 10 | |||
| 11 | |||
| 12 | # we need a delegate and therefore need the proper subclasses | ||
| 13 | 1 | ▶ | use parent qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | Class::Value | ||
| 15 | Class::Scaffold::Storable | ||
| 16 | 1 | ); | |
| 17 | __PACKAGE__->mk_scalar_accessors(qw(name position)); | ||
| 18 | |||
| 19 | # Alternative constructor: only takes a name, sets start position | ||
| 20 | sub pos_name_start { | ||
| 21 | my $self = shift; | ||
| 22 | $self->delegate->STAGE_START; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub pos_name_active { | ||
| 26 | my $self = shift; | ||
| 27 | $self->delegate->STAGE_ACTIVE; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub pos_name_end { | ||
| 31 | my $self = shift; | ||
| 32 | $self->delegate->STAGE_END; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub new_from_name { | ||
| 36 | my ($self, $name, %args) = @_; | ||
| 37 | $self->new( | ||
| 38 | value => sprintf('%s_%s', $self->pos_name_start, $name), | ||
| 39 | %args | ||
| 40 | ); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub new_start { | ||
| 44 | my $self = shift; | ||
| 45 | $self->new_from_name(@_)->set_start; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub new_active { | ||
| 49 | my $self = shift; | ||
| 50 | $self->new_from_name(@_)->set_active; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub new_end { | ||
| 54 | my $self = shift; | ||
| 55 | $self->new_from_name(@_)->set_end; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub get_value { | ||
| 59 | my $self = shift; | ||
| 60 | return unless $self->position && $self->name; | ||
| 61 | sprintf '%s_%s', $self->position, $self->name; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub set_value { | ||
| 65 | my ($self, $value) = @_; | ||
| 66 | my ($position, $name) = $self->split_value($value); | ||
| 67 | $self->position($position); | ||
| 68 | $self->name($name); | ||
| 69 | $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | # expects a string like 'ende_policy' | ||
| 73 | sub is_well_formed_value { | ||
| 74 | my ($self, $value) = @_; | ||
| 75 | $self->SUPER::is_well_formed_value($value) | ||
| 76 | && defined $self->split_value($value); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub split_value { | ||
| 80 | my ($self, $value) = @_; | ||
| 81 | our $pos_re ||= join '|' => | ||
| 82 | ($self->pos_name_start, $self->pos_name_active, $self->pos_name_end); | ||
| 83 | return unless defined($value) && length($value); | ||
| 84 | return unless $value =~ /^($pos_re)_([\w_]+)$/; | ||
| 85 | return ($1, $2); | ||
| 86 | } | ||
| 87 | |||
| 88 | # these methods return $self to allow chaining | ||
| 89 | sub set_start { $_[0]->position($_[0]->pos_name_start); $_[0] } | ||
| 90 | sub set_active { $_[0]->position($_[0]->pos_name_active); $_[0] } | ||
| 91 | sub set_end { $_[0]->position($_[0]->pos_name_end); $_[0] } | ||
| 92 | sub is_start { $_[0]->position eq $_[0]->pos_name_start } | ||
| 93 | sub is_active { $_[0]->position eq $_[0]->pos_name_active } | ||
| 94 | sub is_end { $_[0]->position eq $_[0]->pos_name_end } | ||
| 95 | 1; | ||
| 96 | |||
| 97 | |||
| 98 | __END__ | ||