remove executable bit from tests
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / ShowClass.pm
CommitLineData
1716b200 1use strict;
2use warnings;
e958cbc6 3package Devel::REPL::Plugin::ShowClass;
6f4f9516 4
6a5409bc 5use Devel::REPL::Plugin;
aa8b7647 6use namespace::autoclean;
e958cbc6 7
8has 'metaclass_cache' => (
9 is => 'ro',
10 isa => 'HashRef',
11 lazy => 1,
12 default => sub {{}}
13);
14
15before 'eval' => sub {
16 my $self = shift;
17 $self->update_metaclass_cache;
18};
19
20after 'eval' => sub {
21 my $self = shift;
afc8677b 22
e958cbc6 23 my @metas_to_show;
afc8677b 24
e958cbc6 25 foreach my $class (Class::MOP::get_all_metaclass_names()) {
26 unless (exists $self->metaclass_cache->{$class}) {
27 push @metas_to_show => Class::MOP::get_metaclass_by_name($class)
28 }
afc8677b 29 }
30
e958cbc6 31 $self->display_class($_) foreach @metas_to_show;
afc8677b 32
e958cbc6 33 $self->update_metaclass_cache;
34};
35
36sub update_metaclass_cache {
37 my $self = shift;
38 foreach my $class (Class::MOP::get_all_metaclass_names()) {
39 $self->metaclass_cache->{$class} = (
40 ("" . Class::MOP::get_metaclass_by_name($class))
41 );
afc8677b 42 }
e958cbc6 43}
44
45sub display_class {
46 my ($self, $meta) = @_;
47 $self->print('package ' . $meta->name . ";\n\n");
48 $self->print('extends (' . (join ", " => $meta->superclasses) . ");\n\n") if $meta->superclasses;
afc8677b 49 $self->print('with (' . (join ", " => map { $_->name } @{$meta->roles}) . ");\n\n") if $meta->can('roles');
e958cbc6 50 foreach my $attr (map { $meta->get_attribute($_) } $meta->get_attribute_list) {
51 $self->print('has ' . $attr->name . " => (\n");
afc8677b 52 $self->print(' is => ' . $attr->_is_metadata . ",\n") if $attr->_is_metadata;
53 $self->print(' isa => ' . $attr->_isa_metadata . ",\n") if $attr->_isa_metadata;
54 $self->print(' required => ' . $attr->is_required . ",\n") if $attr->is_required;
55 $self->print(' lazy => ' . $attr->is_lazy . ",\n") if $attr->is_lazy;
56 $self->print(' coerce => ' . $attr->should_coerce . ",\n") if $attr->should_coerce;
57 $self->print(' is_weak_ref => ' . $attr->is_weak_ref . ",\n") if $attr->is_weak_ref;
58 $self->print(' auto_deref => ' . $attr->should_auto_deref . ",\n") if $attr->should_auto_deref;
e958cbc6 59 $self->print(");\n");
60 $self->print("\n");
61 }
62 foreach my $method_name ($meta->get_method_list) {
63 next if $method_name eq 'meta'
64 || $meta->get_method($method_name)->isa('Class::MOP::Method::Accessor');
afc8677b 65 $self->print("sub $method_name { ... }\n");
66 $self->print("\n");
e958cbc6 67 }
afc8677b 68 $self->print("1;\n");
e958cbc6 69}
70
711;
72
cfd1094b 73__END__
74
75=head1 NAME
76
77Devel::REPL::Plugin::ShowClass - Dump classes initialized with Class::MOP
78
79=cut
80