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 @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 |
212 |
|
213 |
L<MooseX::AutoDoc> on which this code is based |
214 |
|
215 |
=cut |
216 |
|
217 |
1; |