| 1 | package Padre::Plugin::Moose::Subtype; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | |||
| 5 | our $VERSION = '0.21'; | ||
| 6 | |||
| 7 | extends 'Padre::Plugin::Moose::ClassMember'; | ||
| 8 | |||
| 9 | with 'Padre::Plugin::Moose::Role::CanGenerateCode'; | ||
| 10 | with 'Padre::Plugin::Moose::Role::CanProvideHelp'; | ||
| 11 | with 'Padre::Plugin::Moose::Role::CanHandleInspector'; | ||
| 12 | |||
| 13 | has 'base_type' => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 14 | has 'constraint' => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 15 | has 'error_message' => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 16 | |||
| 17 | sub generate_moose_code { | ||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | my $code = "subtype '" . $self->name . "'"; | ||
| 21 | $code .= ",\n\tas '" . $self->base_type . "'" if defined $self->base_type && $self->base_type ne ''; | ||
| 22 | $code .= ",\n\twhere { " . $self->constraint . " }" | ||
| 23 | if ( defined $self->constraint && $self->constraint ne '' ) | ||
| 24 | and $self->constraint ne ''; | ||
| 25 | $code .= ",\n\tmessage { \"" . $self->error_message . "\" }" | ||
| 26 | if ( defined $self->error_message ) | ||
| 27 | and $self->error_message ne ''; | ||
| 28 | $code .= ";\n"; | ||
| 29 | |||
| 30 | return $code; | ||
| 31 | } | ||
| 32 | |||
| 33 | # Generate Mouse code! | ||
| 34 | sub generate_mouse_code { | ||
| 35 | return $_[0]->generate_moose_code(@_); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub generate_moosex_declare_code { | ||
| 39 | return $_[0]->generate_moose_code(@_); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub provide_help { | ||
| 43 | require Wx; | ||
| 44 | return Wx::gettext( | ||
| 45 | 'A subtype provides the ability to create custom type constraints to be used in attribute definition.'); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub read_from_inspector { | ||
| 49 | my $self = shift; | ||
| 50 | my $grid = shift; | ||
| 51 | |||
| 52 | my $row = 0; | ||
| 53 | for my $field (qw(name base_type constraint error_message)) { | ||
| 54 | $self->$field( $grid->GetCellValue( $row++, 1 ) ); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub write_to_inspector { | ||
| 59 | my $self = shift; | ||
| 60 | my $grid = shift; | ||
| 61 | |||
| 62 | my $row = 0; | ||
| 63 | for my $field (qw(name base_type constraint error_message)) { | ||
| 64 | $grid->SetCellValue( $row++, 1, $self->$field ); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub get_grid_data { | ||
| 69 | require Wx; | ||
| 70 | return [ | ||
| 71 | { name => Wx::gettext('Name:') }, | ||
| 72 | { name => Wx::gettext('Base Type:'), | ||
| 73 | choices => [ | ||
| 74 | qw(Any Item Bool Maybe[] Undef Defined Value Str Num Int ClassName RoleName Ref ScalarRef[] ArrayRef[] HashRef[] CodeRef RegexpRef GlobRef FileHandle Object) | ||
| 75 | ] | ||
| 76 | }, | ||
| 77 | { name => Wx::gettext('Constraint:') }, | ||
| 78 | { name => Wx::gettext('Error message:') }, | ||
| 79 | ]; | ||
| 80 | } | ||
| 81 | |||
| 82 | __PACKAGE__->meta->make_immutable; | ||
| 83 | |||
| 84 | no Moose; | ||
| 85 | 1; |