| 1 | package Padre::Plugin::Moose::Attribute; | ||
| 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 'access_type' => ( is => 'rw', isa => 'Str' ); | ||
| 14 | has 'type' => ( is => 'rw', isa => 'Str' ); | ||
| 15 | has 'trigger' => ( is => 'rw', isa => 'Str' ); | ||
| 16 | has 'required' => ( is => 'rw', isa => 'Bool' ); | ||
| 17 | has 'class_has' => ( is => 'rw', isa => 'Bool' ); | ||
| 18 | has 'coerce' => ( is => 'rw', isa => 'Bool' ); | ||
| 19 | has 'does' => ( is => 'rw', isa => 'Str' ); | ||
| 20 | has 'weak_ref' => ( is => 'rw', isa => 'Bool' ); | ||
| 21 | has 'lazy' => ( is => 'rw', isa => 'Bool' ); | ||
| 22 | has 'builder' => ( is => 'rw', isa => 'Str' ); | ||
| 23 | has 'default' => ( is => 'rw', isa => 'Str' ); | ||
| 24 | has 'clearer' => ( is => 'rw', isa => 'Str' ); | ||
| 25 | has 'predicate' => ( is => 'rw', isa => 'Str' ); | ||
| 26 | has 'documentation' => ( is => 'rw', isa => 'Str' ); | ||
| 27 | |||
| 28 | my @FIELDS = qw( | ||
| 29 | name access_type type class_has required trigger coerce does weak_ref | ||
| 30 | lazy builder default clearer predicate documentation); | ||
| 31 | |||
| 32 | sub generate_moose_code { | ||
| 33 | my $self = shift; | ||
| 34 | my $options = shift; | ||
| 35 | my $comment = $options->{comments}; | ||
| 36 | |||
| 37 | my $has_code = ''; | ||
| 38 | $has_code .= ( "\tis => '" . $self->access_type . "',\n" ) | ||
| 39 | if defined $self->access_type && $self->access_type ne ''; | ||
| 40 | $has_code .= ( "\tisa => '" . $self->type . "',\n" ) if defined $self->type && $self->type ne ''; | ||
| 41 | $has_code .= ("\trequired => 1,\n") if $self->required; | ||
| 42 | $has_code .= ( "\ttrigger => " . $self->trigger . ",\n" ) if defined $self->trigger && $self->trigger ne ''; | ||
| 43 | |||
| 44 | my $has = ( $self->class_has ) ? 'class_has' : 'has'; | ||
| 45 | return "$has '" . $self->name . "'" . ( $has_code ne '' ? qq{ => (\n$has_code)} : q{} ) . ";\n"; | ||
| 46 | } | ||
| 47 | |||
| 48 | # Generate Mouse code! | ||
| 49 | sub generate_mouse_code { | ||
| 50 | my $self = shift; | ||
| 51 | my $options = shift; | ||
| 52 | my $comment = $options->{comments}; | ||
| 53 | |||
| 54 | my $has_code = ''; | ||
| 55 | $has_code .= ( "\tis => '" . $self->access_type . "',\n" ) | ||
| 56 | if defined $self->access_type && $self->access_type ne ''; | ||
| 57 | $has_code .= ( "\tisa => '" . $self->type . "',\n" ) if defined $self->type && $self->type ne ''; | ||
| 58 | $has_code .= ("\trequired => 1,\n") if $self->required; | ||
| 59 | $has_code .= ( "\ttrigger => " . $self->trigger . ",\n" ) if defined $self->trigger && $self->trigger ne ''; | ||
| 60 | |||
| 61 | return "has '" . $self->name . "'" . ( $has_code ne '' ? qq{ => (\n$has_code)} : q{} ) . ";\n"; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub generate_moosex_declare_code { | ||
| 65 | return $_[0]->generate_moose_code(@_); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub provide_help { | ||
| 69 | require Wx; | ||
| 70 | return Wx::gettext('An attribute is a property that every member of a class has.'); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub read_from_inspector { | ||
| 74 | my $self = shift; | ||
| 75 | my $grid = shift; | ||
| 76 | |||
| 77 | my $row = 0; | ||
| 78 | for my $field (@FIELDS) { | ||
| 79 | $self->$field( $grid->GetCellValue( $row++, 1 ) ); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | sub write_to_inspector { | ||
| 84 | my $self = shift; | ||
| 85 | my $grid = shift; | ||
| 86 | |||
| 87 | my $row = 0; | ||
| 88 | for my $field (@FIELDS) { | ||
| 89 | $grid->SetCellValue( $row++, 1, $self->$field ); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub get_grid_data { | ||
| 94 | require Wx; | ||
| 95 | return [ | ||
| 96 | { name => Wx::gettext('Name:') }, | ||
| 97 | { name => Wx::gettext('Access type:'), choices => [qw(rw ro bare)] }, | ||
| 98 | { name => Wx::gettext('Type:'), | ||
| 99 | choices => [ | ||
| 100 | qw(Any Item Bool Maybe[] Undef Defined Value Str Num Int ClassName RoleName Ref ScalarRef[] ArrayRef[] HashRef[] CodeRef RegexpRef GlobRef FileHandle Object) | ||
| 101 | ] | ||
| 102 | }, | ||
| 103 | { name => Wx::gettext('Class Attribute?'), is_bool => 1 }, | ||
| 104 | { name => Wx::gettext('Required?'), is_bool => 1 }, | ||
| 105 | { name => Wx::gettext('Trigger:') }, | ||
| 106 | { name => Wx::gettext('Coerce?'), is_bool => 1 }, | ||
| 107 | { name => Wx::gettext('Does role:') }, | ||
| 108 | { name => Wx::gettext('Weak Ref?'), is_bool => 1 }, | ||
| 109 | { name => Wx::gettext('lazy?'), is_bool => 1 }, | ||
| 110 | { name => Wx::gettext('Builder:') }, | ||
| 111 | { name => Wx::gettext('Default:') }, | ||
| 112 | { name => Wx::gettext('Clearer:') }, | ||
| 113 | { name => Wx::gettext('Predicate:') }, | ||
| 114 | { name => Wx::gettext('Documentation:') }, | ||
| 115 | ]; | ||
| 116 | } | ||
| 117 | |||
| 118 | __PACKAGE__->meta->make_immutable; | ||
| 119 | |||
| 120 | no Moose; | ||
| 121 | 1; |