/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 133 - (show annotations)
Tue Jul 15 13:58:07 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 4452 byte(s)
 r151@eeepy:  dpavlin | 2008-07-15 15:58:25 +0200
 display required attributes as bold with title "required" instead of separate column

1 package Frey::Introspect;
2
3 use Moose;
4 use Carp;
5 #use Moose::Meta::Role;
6 #use Moose::Meta::Class;
7 use Data::Dump qw/dump/;
8 use File::Slurp;
9 use List::Util;
10
11 use lib 'lib';
12
13 extends 'Frey';
14 with 'Frey::Web';
15
16 has 'package' => (
17 is => 'rw',
18 isa => 'Str',
19 required => 1,
20 );
21
22 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) = @_;
34
35 my ( $class, $meta, $is_role ) = $self->load_package;
36
37 if ( ! $is_role ) {
38 my @superclasses = map{ $_->meta->name }
39 grep { $_ ne 'Moose::Object' } $meta->superclasses;
40 warn "superclasses ",dump( @superclasses ) if $self->debug;
41 }
42
43 my $out;
44
45 my ( $m, $c ) = split(/::/, $class->name, 2);
46 my $filename = $m . '.' . ( $c ? "$c." : '' ) . 'js';
47
48 $out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n";
49
50 foreach ( $class->get_attribute_list ) {
51 $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";
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";
82
83 $out .= "\t}),\n";
84
85 $out =~ s/,\n$/\n/;
86 $out .= "});\n";
87
88 $out .= "\nconsole.log( 'loaded " . $class->name . " from $filename' );\n";
89
90 warn "method_list = ",dump( $class->get_method_list ) if $self->debug;
91
92 # print $out;
93 my $path = "static/blib/$filename";
94 write_file( $path, $out );
95 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 sort @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 ( $class, $meta, $is_role ) = $self->load_package;
142
143 my $package = $self->package;
144
145 my @methods;
146 @methods = map { qq|<td><a href="/~/$package/$_">$_</a></td>| } $self->methods if $class->can('meta');
147
148 my @attributes;
149 if ( $class->get_attribute_list ) {
150 @attributes = map {
151 my $attr = $class->get_attribute($_);
152 my ( $before, $title, $after ) = ( '', '', '' );
153 ( $before, $title, $after ) = ( '<b>', ' title="required"', '</b>' ) if $attr->is_required;
154 qq|<td>$before<a href="/~/$package/$_?"$title>$_</a>$after</td>|
155 } sort $class->get_attribute_list
156 }
157
158 my $table = qq|<table><tr><th>Methods</th><th>Attributes</th></tr>|;
159 while ( @methods || @attributes ) {
160 my ($m,$a) = ( shift @methods, shift @attributes );
161 $m ||= '<td></td>';
162 $a ||= '<td></td>';
163 $table .= qq|<tr>$m$a</tr>|;
164 }
165 $table .= qq|</table>|;
166
167 my $classes =
168 qq|<div class="classes">| .
169 Frey::ClassBrowser->new->markup .
170 qq|</div>|;
171
172 $self->add_css( 'static/introspect.css' );
173
174 warn "## css = ",dump( $self->css );
175
176 my $superclasses = 'Role';
177 $superclasses = 'Superclasses: ' . join(', ',
178 map {
179 my $s = $_->meta->name;
180 qq|<a href="/~/$s">$s</a>|
181 }
182 #grep { $_ ne 'Moose::Object' }
183 $meta->superclasses
184 ) if ! $is_role && $meta->superclasses;
185
186 my $pod = Frey::Pod->new( class => $package )->markup;
187
188 my $html = $self->page(
189 title => "Introspect $package",
190 body => qq|<h1>$package</h1>|
191 . ( $pod ? qq|<a href="#___top" title="Skip to POD" style="font-size: 80%; color: #aaa;">&darr;pod&darr</a> | : '' )
192 . qq|$superclasses\n$table\n$pod\n$classes|,
193
194 );
195
196 $request->print($html);
197 warn "# >>> html ",length($html)," bytes\n";
198 $request->next;
199 }
200 warn "# exit html";
201 }
202
203 =head1 SEE ALSO
204
205 L<MooseX::AutoDoc> on which this code is based
206
207 =cut
208
209 1;

  ViewVC Help
Powered by ViewVC 1.1.26