/[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 50 by dpavlin, Wed Jul 2 22:30:19 2008 UTC revision 116 by dpavlin, Sun Jul 13 18:01:40 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 ) {
         #method and attribute information for the parent class.  
         #this is fragile, but there is not better way that i am aware of  
         my $rmeta = Moose::Meta::Role->meta;  
         $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;  
38                  my @superclasses = map{ $_->meta->name }                  my @superclasses = map{ $_->meta->name }
39                          grep { $_ ne 'Moose::Object' } $meta->superclasses;                          grep { $_ ne 'Moose::Object' } $meta->superclasses;
40                  warn "superclasses ",dump( @superclasses );                  warn "superclasses ",dump( @superclasses ) if $self->debug;
41          }          }
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    
# Line 84  sub examine { Line 72  sub examine {
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";
# Line 93  sub examine { Line 87  sub examine {
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 );  
91    
92          warn dump( $class->get_method_list );  #       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                                    ul => [
159                                            map { (
160                                                    li => [ a => { href => '/~/' . $self->package . '/' . $_ } => [ $_ ] ]
161                                            ) } $self->methods
162                                    ]
163                            );
164                            $methods = "<h2>Methods</h2>$methods" if $methods;
165                    } else {
166                            $methods = '<b>not introspectable</b>';
167                    }
168    
169                    my $attributes;
170                    if ( $class->get_attribute_list ) {
171                            $attributes = dom2html(
172                                    table => [
173                                            map {
174                                                    my $attr = $class->get_attribute($_);
175                                                    warn "## $_ ", $attr->is_required ? 'required' : 'optional';
176                                                    ( tr => [
177                                                            td => [ a => { href => '/~/' . $self->package . '/' . $_ } => [ $_ ] ],
178                                                            td => [ $attr->is_required ? ' <b>required</b>' : '' ],
179                                                    ] )
180                                            } $class->get_attribute_list
181                                    ],
182                            );
183                            $attributes = "<h2>Attributes</h2>$attributes" if $attributes;
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.50  
changed lines
  Added in v.116

  ViewVC Help
Powered by ViewVC 1.1.26