/[Frey]/trunk/lib/Frey/Introspect.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/Frey/Introspect.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 49 by dpavlin, Wed Jul 2 21:10:57 2008 UTC revision 100 by dpavlin, Fri Jul 11 19:19:42 2008 UTC
# Line 2  package Frey::Introspect; Line 2  package Frey::Introspect;
2    
3  use Moose;  use Moose;
4  use Carp;  use Carp;
5  use Class::MOP;  #use Moose::Meta::Role;
6  use Moose::Meta::Role;  #use Moose::Meta::Class;
 use Moose::Meta::Class;  
 use Scalar::Util qw/blessed/;  
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use File::Slurp;  use File::Slurp;
9    use List::Util;
10    
11    use lib 'lib';
12    
13  extends 'Frey';  extends 'Frey';
14    with 'Frey::Web';
15    
16  has 'package' => (  has 'package' => (
17          is => 'rw',          is => 'rw',
# Line 17  has 'package' => ( Line 19  has 'package' => (
19          required => 1,          required => 1,
20  );  );
21    
22  sub examine {  has 'path' => (
23            is => 'rw',
24    );
25    
26    =head2 joose
27    
28      my $js = $o->joose( 'Some::Package' );
29    
30    =cut
31    
32    sub joose {
33          my ($self) = @_;          my ($self) = @_;
34    
35          my $package = $self->package;          my ( $class, $meta, $is_role ) = $self->load_package;
36    
37    #intercept role application so we can accurately generate          if ( ! $is_role ) {
38    #method and attribute information for the parent class.                  my @superclasses = map{ $_->meta->name }
39    #this is fragile, but there is not better way that i am aware of                          grep { $_ ne 'Moose::Object' } $meta->superclasses;
40    my $rmeta = Moose::Meta::Role->meta;                  warn "superclasses ",dump( @superclasses ) if $self->debug;
41    $rmeta->make_mutable if $rmeta->is_immutable;          }
   my $original_apply = $rmeta->get_method("apply")->body;  
   $rmeta->remove_method("apply");  
   my @roles_to_apply;  
   $rmeta->add_method("apply", sub{push(@roles_to_apply, [@_])});  
   #load the package with the hacked Moose::Meta::Role  
   eval { Class::MOP::load_class($package); };  
   confess "Failed to load package ${package} $@" if $@;  
   
   #get on with analyzing the  package  
   my $meta = $package->meta;  
   my $spec = {};  
   my ($class, $is_role);  
   if($package->meta->isa('Moose::Meta::Role')){  
     $is_role = 1;  
     # we need to apply the role to a class to be able to properly introspect it  
     $class = Moose::Meta::Class->create_anon_class;  
     $original_apply->($meta, $class);  
   } else {  
     #roles don't have superclasses ...  
     $class = $meta;  
     my @superclasses = map{ $_->meta }  
       grep { $_ ne 'Moose::Object' } $meta->superclasses;  
         warn "superclasses ",dump( @superclasses );  
   }  
42    
43          my $out;          my $out;
44    
45          my ( $m, $c ) = split(/::/, $class->name, 2);          my ( $m, $c ) = split(/::/, $class->name, 2);
46          my $filename = "$m.$c.js";          my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js';
47    
48          $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";          $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";
49    
50          foreach ( $class->get_attribute_list ) {          foreach ( $class->get_attribute_list ) {
51                  $out .= "\t\t\t$_: {\n";                  $out .= "\t\t\t$_: {\n";
52    
53                    my $attr = $class->get_attribute($_);
54                    my $is = $attr->_is_metadata;
55                    $out .= "\t\t\t\tis: \"$is\",\n" if defined $is;
56                    $out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy;
57                    $out .= "\t\t\t\trequired: true,\n" if $attr->is_required;
58                    $out .= "\t\t\t\tinit: \"" . $attr->init_arg . "\",\n" if $attr->init_arg;      # FIXME
59    
60                    if( defined(my $isa = $attr->_isa_metadata) ){
61                            if( blessed $isa ){
62                                    while( blessed $isa ){
63                                            $isa = $isa->name;
64                                    }
65                            }
66                            $isa =~ s/\s+\|\s+undef//gi;
67                            $out .= "\t\t\t\tisa: Moose.$isa,\n";
68                    }
69    
70    
71                  $out .= "\t\t\t},\n";                  $out .= "\t\t\t},\n";
72    
73          }          }
74    
75            $out .= "\t\t},\n\t\tmeta: Frey.HTML,
76                    classMethods: {
77                            renderHTML: function () {
78                                    return new Joose.SimpleRequest().getText(\"/~/" . $self->package . "\")
79                            },\n";
80    
81          $out .= "\t\t},\n";          $out .= "\t\t},\n";
82    
83          $out .= "\t}),\n";          $out .= "\t}),\n";
84    
85            $out =~ s/,\n$/\n/;
86          $out .= "});\n";          $out .= "});\n";
87    
88          $out .= "\nconsole.log( 'loaded " . $class->name . " from $filename' );\n";          $out .= "\nconsole.log( 'loaded " . $class->name . " from $filename' );\n";
89    
90          warn $class->dump(2);          warn "method_list = ",dump( $class->get_method_list ) if $self->debug;
   
         warn "get_attribute_list = ",dump( $class->get_attribute_list );  
 #       warn dump( map{ $class->get_attribute($_) } sort $class->get_attribute_list );  
   
         warn dump( $class->get_method_list );  
91    
92          print $out;  #       print $out;
93          my $path = "static/blib/$filename";          my $path = "static/blib/$filename";
94          write_file( $path, $out );          write_file( $path, $out );
95          warn "# created $path\n";          warn "# created $path\n";
96            $self->path( $path );
97    
98            return $out;
99    }
100    
101    =head2 methods
102    
103      my @methods = $o->methods;
104    
105    =cut
106    
107    sub methods {
108            my $self = shift;
109    
110            my ( $class, $meta, $is_role ) = $self->load_package;
111    
112            my $attr;
113            $attr->{$_}++ foreach $class->get_attribute_list;
114            my @methods = grep { ! defined($attr->{$_}) } $class->get_method_list;
115            warn "# methods = ",dump( @methods ) if $self->debug;
116    
117            return @methods;
118    }
119    
120    use Frey::ClassLoader;
121    
122    sub load_package {
123            my $self = shift;
124            return Frey::ClassLoader->load_package( $self->package );
125    }
126    
127    
128    =head1 OUTPUT GENERATION
129    
130    =head2 html
131    
132      $o->html( $request );
133    
134    =cut
135    
136    sub html {
137            my ( $self, $request ) = @_;
138    
139            while (1) {
140    
141                    my $js = $self->head_javascript;
142                    $js .= << '__END_OF_JS__';
143    <script type="text/javascript">
144    joose.loadComponents("../lib")
145    
146    function $(id) {
147            return document.getElementById(id)
148    }
149    
150    </script>
151    __END_OF_JS__
152    
153                    my ( $class, $meta, $is_role ) = $self->load_package;
154    
155                    my $methods;
156                    if ( $class->can('meta') ) {
157                            $methods = dom2html(
158                                    h2 => [ 'Methods' ],
159                                    ul => [
160                                            map { (
161                                                    li => [ a => { href => '/~/' . $self->package . '/' . $_ } => [ $_ ] ]
162                                            ) } $self->methods
163                                    ]
164                            );
165                    } else {
166                            $methods = '<b>not introspectable</b>';
167                    }
168    
169                    my $attributes;
170                    if ( $class->get_attribute_list ) {
171                            $attributes = dom2html(
172                                    h2 => [ 'Atrributes' ],
173                                    table => [
174                                            map {
175                                                    my $attr = $class->get_attribute($_);
176                                                    warn "## $_ ", $attr->is_required ? 'required' : 'optional';
177                                                    ( tr => [
178                                                            td => [ a => { href => '/~/' . $self->package . '/' . $_ } => [ $_ ] ],
179                                                            td => [ $attr->is_required ? ' <b>required</b>' : '' ],
180                                                    ] )
181                                            } $class->get_attribute_list
182                                    ],
183                            );
184                    } else {
185                            $attributes = '<b>no attributes</b>';
186                    }
187    
188    
189                    my $html = dom2html(
190                            html => [
191                                    head => [
192                                            link => { rel=>"stylesheet", href=>"/static/app.css", type=>"text/css", media=>"screen" },
193                                            $js,
194                                            title => [ 'Introspect ', $self->package ],
195                                    ],
196                                    body => [
197                                            h1 => [ $self->package ],
198                                            $methods,
199                                            $attributes,
200                                    ],
201                            ]
202                    );
203    
204                    $request->print($html);
205                    warn "# >>> html ",length($html)," bytes\n";
206                    $request->next;
207            }
208            warn "# exit html";
209  }  }
210    
211  =head1 SEE ALSO  =head1 SEE ALSO

Legend:
Removed from v.49  
changed lines
  Added in v.100

  ViewVC Help
Powered by ViewVC 1.1.26