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