/[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 121 - (show annotations)
Mon Jul 14 21:22:43 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3962 byte(s)
central page creation [0.11]

- make (optional) development feature in Makefile.PL
- split page generation in Frey::Web->page
- cleanup of html generation code all over
- jump to Carp::REPL on errors (nice, but needs to be in browser)

This is a move to more embedded html. While it does seem evil, we are aming
here at lean framework, so readability is prefered...
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 # warn "## $_ ", $attr->is_required ? 'required' : 'optional';
153 qq|<td><a href="/~/$package/$_?">$_</a></td><td>| .
154 ( $attr->is_required ? ' <b>required</b>' : '' ) .
155 qq|</td>|;
156 } sort $class->get_attribute_list
157 }
158
159 my $table = qq|<table><tr><th>Methods</th><th>Attributes</th></tr>|;
160 while ( @methods || @attributes ) {
161 my ($m,$a) = ( shift @methods, shift @attributes );
162 $m ||= '<td></td>';
163 $a ||= '<td></td>';
164 $table .= qq|<tr>$m$a</tr>|;
165 }
166 $table .= qq|</table>|;
167
168 my $classes =
169 qq|<div class="classes">| .
170 Frey::ClassBrowser->new->markup .
171 qq|</div>|;
172
173 $self->add_css( 'static/introspect.css' );
174
175 warn "## css = ",dump( $self->css );
176
177 my $html = $self->page(
178 title => "Introspect $package",
179 body => "<h1>$package</h1>\n$table\n$classes",
180 );
181
182 $request->print($html);
183 warn "# >>> html ",length($html)," bytes\n";
184 $request->next;
185 }
186 warn "# exit html";
187 }
188
189 =head1 SEE ALSO
190
191 L<MooseX::AutoDoc> on which this code is based
192
193 =cut
194
195 1;

  ViewVC Help
Powered by ViewVC 1.1.26