| 1 | package Padre::Plugin::Moose::Class; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | |||
| 5 | our $VERSION = '0.21'; | ||
| 6 | |||
| 7 | with 'Padre::Plugin::Moose::Role::CanGenerateCode'; | ||
| 8 | with 'Padre::Plugin::Moose::Role::HasClassMembers'; | ||
| 9 | with 'Padre::Plugin::Moose::Role::CanProvideHelp'; | ||
| 10 | with 'Padre::Plugin::Moose::Role::CanHandleInspector'; | ||
| 11 | |||
| 12 | has 'name' => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 13 | has 'superclasses' => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 14 | has 'roles' => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 15 | has 'immutable' => ( is => 'rw', isa => 'Bool' ); | ||
| 16 | has 'namespace_autoclean' => ( is => 'rw', isa => 'Bool' ); | ||
| 17 | has 'singleton' => ( is => 'rw', isa => 'Bool' ); | ||
| 18 | |||
| 19 | sub generate_moose_code { | ||
| 20 | my $self = shift; | ||
| 21 | my $options = shift; | ||
| 22 | my $comments = $options->{comments}; | ||
| 23 | my $namespace_autoclean = $options->{namespace_autoclean}; | ||
| 24 | my $class = $self->name; | ||
| 25 | my $superclasses = $self->superclasses; | ||
| 26 | my $roles = $self->roles; | ||
| 27 | my $make_immutable = $self->immutable; | ||
| 28 | |||
| 29 | $class =~ s/^\s+|\s+$//g; | ||
| 30 | $superclasses =~ s/^\s+|\s+$//g; | ||
| 31 | $roles =~ s/^\s+|\s+$//g; | ||
| 32 | my @roles = split /,/, $roles; | ||
| 33 | |||
| 34 | my $code = "package $class;\n"; | ||
| 35 | |||
| 36 | $code .= "\nuse Moose;"; | ||
| 37 | |||
| 38 | $code .= | ||
| 39 | $comments | ||
| 40 | ? " # automatically turns on strict and warnings\n" | ||
| 41 | : "\n"; | ||
| 42 | |||
| 43 | if ($namespace_autoclean) { | ||
| 44 | $code .= "use namespace::clean;"; | ||
| 45 | $code .= | ||
| 46 | $comments | ||
| 47 | ? " # Keep imports out of your namespace\n" | ||
| 48 | : "\n"; | ||
| 49 | } | ||
| 50 | |||
| 51 | # If there is at least one subtype, we need to add this import | ||
| 52 | if ( scalar @{ $self->subtypes } ) { | ||
| 53 | $code .= "use Moose::Util::TypeConstraints;\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | # Singleton via MooseX::Singleton | ||
| 57 | $code .= "use MooseX::Singleton;\n" if $self->singleton; | ||
| 58 | |||
| 59 | # Class attributes via MooseX::ClassAttribute | ||
| 60 | for my $attribute ( @{ $self->attributes } ) { | ||
| 61 | if ( $attribute->class_has ) { | ||
| 62 | $code .= "use MooseX::ClassAttribute;\n" if $attribute->class_has; | ||
| 63 | last; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | $code .= "\nextends '$superclasses';\n" if $superclasses ne ''; | ||
| 68 | |||
| 69 | $code .= "\n" if scalar @roles; | ||
| 70 | for my $role (@roles) { | ||
| 71 | $code .= "with '$role';\n"; | ||
| 72 | } | ||
| 73 | |||
| 74 | # Generate class members | ||
| 75 | $code .= $self->to_class_members_code($options); | ||
| 76 | |||
| 77 | if ($make_immutable) { | ||
| 78 | $code .= "\n__PACKAGE__->meta->make_immutable;"; | ||
| 79 | $code .= | ||
| 80 | $comments | ||
| 81 | ? " # Makes it faster at the cost of startup time\n" | ||
| 82 | : "\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | if ($namespace_autoclean) { | ||
| 86 | $code .= "\n1;\n\n"; | ||
| 87 | } else { | ||
| 88 | if ( scalar @{ $self->subtypes } ) { | ||
| 89 | $code .= "\nno Moose::Util::TypeConstraints;\n"; | ||
| 90 | } | ||
| 91 | $code .= "\nno Moose;\n1;\n\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | return $code; | ||
| 95 | } | ||
| 96 | |||
| 97 | # Generate Mouse code! | ||
| 98 | sub generate_mouse_code { | ||
| 99 | my $self = shift; | ||
| 100 | my $options = shift; | ||
| 101 | my $comments = $options->{comments}; | ||
| 102 | my $namespace_autoclean = $options->{namespace_autoclean}; | ||
| 103 | my $class = $self->name; | ||
| 104 | my $superclasses = $self->superclasses; | ||
| 105 | my $roles = $self->roles; | ||
| 106 | my $make_immutable = $self->immutable; | ||
| 107 | |||
| 108 | $class =~ s/^\s+|\s+$//g; | ||
| 109 | $superclasses =~ s/^\s+|\s+$//g; | ||
| 110 | $roles =~ s/^\s+|\s+$//g; | ||
| 111 | my @roles = split /,/, $roles; | ||
| 112 | |||
| 113 | my $code = "package $class;\n"; | ||
| 114 | |||
| 115 | $code .= "\nuse Mouse;"; | ||
| 116 | |||
| 117 | $code .= | ||
| 118 | $comments | ||
| 119 | ? " # automatically turns on strict and warnings\n" | ||
| 120 | : "\n"; | ||
| 121 | |||
| 122 | if ($namespace_autoclean) { | ||
| 123 | $code .= "use namespace::clean;"; | ||
| 124 | $code .= | ||
| 125 | $comments | ||
| 126 | ? " # Keep imports out of your namespace\n" | ||
| 127 | : "\n"; | ||
| 128 | } | ||
| 129 | |||
| 130 | # If there is at least one subtype, we need to add this import | ||
| 131 | if ( scalar @{ $self->subtypes } ) { | ||
| 132 | $code .= "use Mouse::Util::TypeConstraints;\n"; | ||
| 133 | } | ||
| 134 | |||
| 135 | $code .= "\nextends '$superclasses';\n" if $superclasses ne ''; | ||
| 136 | |||
| 137 | $code .= "\n" if scalar @roles; | ||
| 138 | for my $role (@roles) { | ||
| 139 | $code .= "with '$role';\n"; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Generate class members | ||
| 143 | $code .= $self->to_class_members_code($options); | ||
| 144 | |||
| 145 | if ($make_immutable) { | ||
| 146 | $code .= "\n__PACKAGE__->meta->make_immutable;"; | ||
| 147 | $code .= | ||
| 148 | $comments | ||
| 149 | ? " # Makes it faster at the cost of startup time\n" | ||
| 150 | : "\n"; | ||
| 151 | } | ||
| 152 | |||
| 153 | if ($namespace_autoclean) { | ||
| 154 | $code .= "\n1;\n\n"; | ||
| 155 | } else { | ||
| 156 | if ( scalar @{ $self->subtypes } ) { | ||
| 157 | $code .= "\nno Mouse::Util::TypeConstraints;\n"; | ||
| 158 | } | ||
| 159 | $code .= "\nno Mouse;\n1;\n\n"; | ||
| 160 | } | ||
| 161 | |||
| 162 | return $code; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub generate_moosex_declare_code { | ||
| 166 | my $self = shift; | ||
| 167 | my $options = shift; | ||
| 168 | my $comments = $options->{comments}; | ||
| 169 | my $class = $self->name; | ||
| 170 | my $superclasses = $self->superclasses; | ||
| 171 | my $roles = $self->roles; | ||
| 172 | my $make_immutable = $self->immutable; | ||
| 173 | |||
| 174 | $class =~ s/^\s+|\s+$//g; | ||
| 175 | $superclasses =~ s/^\s+|\s+$//g; | ||
| 176 | $roles =~ s/^\s+|\s+$//g; | ||
| 177 | my @roles = split /,/, $roles; | ||
| 178 | |||
| 179 | my $class_body = ''; | ||
| 180 | |||
| 181 | # If there is at least one subtype, we need to add this import | ||
| 182 | if ( scalar @{ $self->subtypes } ) { | ||
| 183 | $class_body .= "use Moose::Util::TypeConstraints;\n"; | ||
| 184 | } | ||
| 185 | |||
| 186 | # Generate class members | ||
| 187 | $class_body .= $self->to_class_members_code($options); | ||
| 188 | |||
| 189 | my @lines = split /\n/, $class_body; | ||
| 190 | for my $line (@lines) { | ||
| 191 | $line = "\t$line" if $line ne ''; | ||
| 192 | } | ||
| 193 | $class_body = join "\n", @lines; | ||
| 194 | |||
| 195 | my $extends = ( $superclasses ne '' ) ? "extends ($superclasses) " : q{}; | ||
| 196 | my $with = ( scalar @roles ) ? "with ($roles) " : q{}; | ||
| 197 | my $mutable = $make_immutable ? q{} : "is mutable "; | ||
| 198 | |||
| 199 | # namespace::autoclean is implicit in { } | ||
| 200 | return "use MooseX::Declare;\nclass $class $extends$with$mutable\{\n$class_body\n}\n"; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub provide_help { | ||
| 204 | require Wx; | ||
| 205 | return Wx::gettext( | ||
| 206 | ' A class is a blueprint of how to create objects of itself. A class can contain attributes, subtypes and methods which enable objects to have state and behavior.' | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub read_from_inspector { | ||
| 211 | my $self = shift; | ||
| 212 | my $grid = shift; | ||
| 213 | |||
| 214 | my $row = 0; | ||
| 215 | for my $field (qw(name superclasses roles immutable singleton)) { | ||
| 216 | $self->$field( $grid->GetCellValue( $row++, 1 ) ); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | sub write_to_inspector { | ||
| 221 | my $self = shift; | ||
| 222 | my $grid = shift; | ||
| 223 | |||
| 224 | my $row = 0; | ||
| 225 | for my $field (qw(name superclasses roles immutable singleton)) { | ||
| 226 | $grid->SetCellValue( $row++, 1, $self->$field ); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | sub get_grid_data { | ||
| 231 | require Wx; | ||
| 232 | return [ | ||
| 233 | { name => Wx::gettext('Name:') }, | ||
| 234 | { name => Wx::gettext('Superclasses:') }, | ||
| 235 | { name => Wx::gettext('Roles:') }, | ||
| 236 | { name => Wx::gettext('Make immutable?'), is_bool => 1 }, | ||
| 237 | { name => Wx::gettext('Singleton?'), is_bool => 1 }, | ||
| 238 | ]; | ||
| 239 | } | ||
| 240 | |||
| 241 | __PACKAGE__->meta->make_immutable; | ||
| 242 | |||
| 243 | no Moose; | ||
| 244 | 1; |