--- trunk/lib/Frey/Introspect.pm 2008/12/05 17:34:42 727
+++ trunk/lib/Frey/Introspect.pm 2009/06/30 15:10:55 1133
@@ -11,13 +11,17 @@
use PPI;
use PPI::HTML;
-use Frey::Pod;
-
use lib 'lib';
+use Frey::Pod;
extends 'Frey::PPI';
-with 'Frey::Web';
-with 'Frey::Storage';
+with 'Frey::Web', 'Frey::Storage';
+
+=head1 DESCRIPTION
+
+Provide introspection on any perl class installed on system
+
+=cut
has 'class' => (
is => 'rw',
@@ -55,7 +59,9 @@
$out .= "\t\t\t$_: {\n";
my $attr = $meta->get_attribute($_);
- my $is = $attr->_is_metadata;
+ my $is = eval { $attr->_is_metadata; };
+ return if $@;
+
$out .= "\t\t\t\tis: \"$is\",\n" if defined $is;
$out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy;
$out .= "\t\t\t\trequired: true,\n" if $attr->is_required;
@@ -104,15 +110,14 @@
sub as_markup {
my ( $self ) = @_;
- $self->add_head( 'static/introspect.css' );
-
my ( $meta, $is_role ) = $self->class_meta;
my $class = $self->class;
+ $self->title( $class );
my $introspect_path = "var/introspect/$class.yaml";
$self->mkbasepath( $introspect_path );
- my $introspect = $self->load( $introspect_path );
+ my $introspect; # FIXME update with = $self->load( $introspect_path );
my ( $superclasses, $roles ) = ( 'Role', '' );
if ( ! $is_role ) {
@@ -121,8 +126,10 @@
join(', ',
map {
my $name = $_->meta->name;
- $introspect->{superclass}->{$name}++;
- $self->dropdown( $name, $_->meta )
+ $introspect->{superclass}->{$name} = {};
+
+ qq|$name| .
+ $self->dropdown( qq|?|, $_->meta )
}
#grep { $_ ne 'Moose::Object' }
$meta->superclasses
@@ -130,50 +137,67 @@
}
}
- my $role_method;
- my $role_attribute;
+ my $method_from_role;
+ my $attribute_from_role;
if ( $meta->can('roles') ) {
my $role_nr = 1;
$roles = join(' ',
grep { ! m/\Q$class\E/ } # skip me
map {
- my $name = $_->name;
- $introspect->{role}->{$name}++;
- $role_method->{ $_ }->{$name} = $role_nr foreach $_->get_method_list;
- $role_attribute->{ $_ }->{$name} = $role_nr foreach $_->get_attribute_list;
- $self->dropdown( $name, $name->meta ) . qq|| . $role_nr++ . qq||;
+ my $r = '';
+ foreach my $name ( split(/\|/, $_->name) ) {
+ $introspect->{roles}->{$name} = {};
+ $method_from_role->{ $_ }->{$name} = $role_nr foreach $_->get_method_list;
+ $attribute_from_role->{ $_ }->{$name} = $role_nr foreach $_->get_attribute_list;
+
+ $r .=
+ qq|$name|
+ # . qq|| . $self->dropdown( $role_nr++, $name->meta ) . qq||
+ . $self->dropdown(
+ qq|| . $role_nr++ . qq||,
+ $name->meta
+ )
+ ;
+ }
+ $r;
}
$meta->calculate_all_roles
);
- $roles = qq| with roles: $roles| if $roles;
+ $roles = qq|with roles: $roles| if $roles;
}
- warn "# role_method ",dump( $role_method );
+ warn "# method_from_role ",dump( $method_from_role );
my @methods;
@methods = map {
my $name = $_;
- $introspect->{method}->{$name}++;
- if ( $role_method->{$name} ) {
- my ( $role_name, $nr ) = each %{ $role_method->{$name} };
+ if ( $method_from_role->{$name} ) {
+ my ( $role_name, $nr ) = each %{ $method_from_role->{$name} };
+ $introspect->{methods}->{$name}->{role} = $role_name;
$name .= qq|$nr|;
+ } else {
+ $introspect->{methods}->{$name} = {};
}
qq|
$name | |
- } sort $self->class_methods( $class );
+ } sort {
+ lc($a) cmp lc($b)
+ } $self->class_methods( $class );
my @attributes;
if ( $meta->get_attribute_list ) {
@attributes = map {
my $name = $_;
- $introspect->{attribute}->{$name}++;
+ $introspect->{attribute}->{$name} = {};
my $html_name = $name;
my $attr = $meta->get_attribute($name);
confess "$class attribute $name isn't blessed ",dump( $attr ) unless blessed $attr;
warn "## attr $name ref ",ref( $attr ) if $self->debug;
my ( $title, $properties ) = ( '', '' );
- ( $html_name, $title ) = ( "$name", ' title="required"' )
- if $attr->can('is_required') && $attr->is_required;
+ if ( $attr->can('is_required') && $attr->is_required ) {
+ ( $html_name, $title ) = ( "$name", ' title="required"' );
+ $introspect->{attribute}->{$name}->{required} = 1;
+ }
foreach my $check ( qw/has_type_constraint has_handles is_weak_ref is_required is_lazy should_coerce should_auto_deref has_default has_trigger has_applied_traits/ ) {
my $getter;
@@ -182,25 +206,40 @@
$getter =~ s/^has_//;
if ( $attr->can($check) && $attr->$check ) {
- if ( $getter eq $check ) {
- $properties .= "$check";
- } else {
- # we need our dump here instead of $attr->$getter->dump because default can return scalar
- $properties .= $self->dropdown( $check, $attr->$getter );
- }
+ $properties .= $check;
+ # we need our dump here instead of $attr->$getter->dump because default can return scalar
+ my $v = $attr->$getter;
+ $properties .= ref($v)
+ ? $self->dropdown( qq|?|, $attr->$getter )
+ : qq| $v
|
+ ;
}
$properties .= ' ';
}
my $type = $attr->can('has_type_constraint') && $attr->has_type_constraint ? $attr->type_constraint->name : '';
- if ( $role_attribute->{$name} ) {
- my ( $role_name, $nr ) = each %{ $role_attribute->{$name} };
+ if ( $attribute_from_role->{$name} ) {
+ my ( $role_name, $nr ) = each %{ $attribute_from_role->{$name} };
$name .= qq|$nr|;
}
- eval { $properties = $attr->documentation . ' ' . $properties };
+ if ( my $doc = eval { $attr->documentation } ) {
+ $properties = qq|
+ $properties
+ $doc
+ |;
+ $self->add_css(qq|
+ span.documentation {
+ background: #eee;
+ padding: 0.25em;
+ float: left;
+ clear: left;
+ }
+ |);
+ $introspect->{action}->{$name}->{documentation} = $doc;
+ }
- qq|$html_name | $type | $properties | |
+ qq|$html_name | $type | $properties | |
} sort $meta->get_attribute_list
}
@@ -215,8 +254,10 @@
my $path = $self->class_path( $class );
- my $pod = Frey::Pod->new( class => $class, request_url => $self->request_url )->as_markup;
- return $pod if $path =~ m{\.pod};
+ my ( $pod_toc, $pod ) = Frey::Pod->new( class => $class, request_url => $self->request_url )->as_markup;
+ return $pod_toc . $pod if $path =~ m{\.pod};
+
+ warn "# ", $pod_toc ? 'toc' : '', ' ', $pod ? 'pod' : '';
my $Document = PPI::Document->new( $path );
@@ -239,39 +280,112 @@
# $source =~ s{^.*]+>}{}s;
# $source =~ s{",
map {
- $introspect->{runnable}->{$_}++;
+ $introspect->{runnable}->{$_} = {};
my $short = $_;
$short =~ s{_as_(?:markup|data|sponge)$}{};
qq|$short|
} $self->class_runnable( $class )
);
- $runnable = "
run: $runnable" if $runnable;
-
- $self->store( $introspect_path, $introspect );
-
- $self->title( $class );
+ $runnable = "runnable$runnable" if $runnable;
my $has_tests = '';
- my @tests = grep { defined $_ } $self->has_tests;
+ my @tests = sort { lc($a) cmp lc($b) } grep { defined $_ } $self->has_tests;
if ( @tests ) {
- $has_tests =
- '
test' . ( $#tests > 0 ? 's' : '' ) . ': ' .
- join("\n", map {
- qq|$_|
- } @tests );
+ $has_tests
+ = qq|test|
+ . ( $#tests > 0 ? 's' : '' )
+ . qq||
+ . join("", map {
+ qq|$_|
+ } @tests )
+ . qq||
+ ;
+ $introspect->{tests} = [ @tests ],
}
- return join("\n",
- qq|$class
|,
- qq|$superclasses\n$roles\n$runnable\n|,
- $has_tests,
- $pod ? qq|
pod| : '',
- $source ? qq|
source| : '',
- qq|$table\n$pod\n
\n|,
- qq|Source
$source
|,
- );
+ my $includes = '';
+ if ( my $inc = $self->includes ) {
+ $introspect->{includes} = $inc;
+ foreach my $type ( keys %$inc ) {
+ $includes
+ .= ucfirst($type)
+ . qq|: |
+ . join("\n",
+ map {
+ qq|$_|
+ } @{
+ $inc->{$type}
+ }
+ )
+ ;
+ }
+ }
+
+ $self->store( $introspect_path, $introspect );
+
+ $self->add_css(qq|
+ .frey-introspect-right {
+ position: fixed;
+ top: 1em;
+ right: 1em;
+ z-index: 10;
+ background: #ffc;
+ padding: 0.5em;
+ width: 20%;
+ font-size: 80%;
+ }
+ .frey-introspect-right dl dd {
+ margin-left: 1em;
+ }
+
+ /* fix pod */
+ .frey-introspect-right dd ul {
+ padding-left: 0;
+ }
+ .frey-introspect-right dl ul > li {
+ list-style: none;
+ }
+ |);
+
+ my $has_pod = qq|
+ pod
+ $pod_toc
+ | if $pod_toc;
+
+ my $has_source = qq|
+ source
+ | if $source;
+
+ my $right = qq|
+
+ $runnable
+ $has_tests
+ $has_pod
+ $has_source
+
+ |;
+
+
+ return
+ qq|
+ $class
+
+ $superclasses $roles
+
$includes
+
+
+ $right
+
+ $table
+ $pod
+
+
+ Source
+ $source
+ |
+ ;
}
=head1 SEE ALSO
@@ -280,4 +394,7 @@
=cut
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
1;