1 |
package Frey::Introspect; |
2 |
|
3 |
use Moose; |
4 |
use Carp; |
5 |
use Class::MOP; |
6 |
use Moose::Meta::Role; |
7 |
use Moose::Meta::Class; |
8 |
use Scalar::Util qw/blessed/; |
9 |
use Data::Dump qw/dump/; |
10 |
use File::Slurp; |
11 |
|
12 |
extends 'Frey'; |
13 |
|
14 |
has 'package' => ( |
15 |
is => 'rw', |
16 |
isa => 'Str', |
17 |
required => 1, |
18 |
); |
19 |
|
20 |
sub examine { |
21 |
my ($self) = @_; |
22 |
|
23 |
my $package = $self->package; |
24 |
|
25 |
#intercept role application so we can accurately generate |
26 |
#method and attribute information for the parent class. |
27 |
#this is fragile, but there is not better way that i am aware of |
28 |
my $rmeta = Moose::Meta::Role->meta; |
29 |
$rmeta->make_mutable if $rmeta->is_immutable; |
30 |
my $original_apply = $rmeta->get_method("apply")->body; |
31 |
$rmeta->remove_method("apply"); |
32 |
my @roles_to_apply; |
33 |
$rmeta->add_method("apply", sub{push(@roles_to_apply, [@_])}); |
34 |
#load the package with the hacked Moose::Meta::Role |
35 |
eval { Class::MOP::load_class($package); }; |
36 |
confess "Failed to load package ${package} $@" if $@; |
37 |
|
38 |
#get on with analyzing the package |
39 |
my $meta = $package->meta; |
40 |
my $spec = {}; |
41 |
my ($class, $is_role); |
42 |
if($package->meta->isa('Moose::Meta::Role')){ |
43 |
$is_role = 1; |
44 |
# we need to apply the role to a class to be able to properly introspect it |
45 |
$class = Moose::Meta::Class->create_anon_class; |
46 |
$original_apply->($meta, $class); |
47 |
} else { |
48 |
#roles don't have superclasses ... |
49 |
$class = $meta; |
50 |
my @superclasses = map{ $_->meta->name } |
51 |
grep { $_ ne 'Moose::Object' } $meta->superclasses; |
52 |
warn "superclasses ",dump( @superclasses ); |
53 |
} |
54 |
|
55 |
my $out; |
56 |
|
57 |
my ( $m, $c ) = split(/::/, $class->name, 2); |
58 |
my $filename = "$m.$c.js"; |
59 |
|
60 |
$out .= "Module(\"$m\", function (m) {\n\tClass(\"$c\", {\n\t\thas: {\n"; |
61 |
|
62 |
foreach ( $class->get_attribute_list ) { |
63 |
$out .= "\t\t\t$_: {\n"; |
64 |
|
65 |
my $attr = $class->get_attribute($_); |
66 |
my $is = $attr->_is_metadata; |
67 |
$out .= "\t\t\t\tis: \"$is\",\n" if defined $is; |
68 |
$out .= "\t\t\t\tlazy: true,\n" if $attr->is_lazy; |
69 |
$out .= "\t\t\t\trequired: true,\n" if $attr->is_required; |
70 |
$out .= "\t\t\t\tinit: \"" . $attr->init_arg . "\",\n" if $attr->init_arg; # FIXME |
71 |
|
72 |
if( defined(my $isa = $attr->_isa_metadata) ){ |
73 |
if( blessed $isa ){ |
74 |
while( blessed $isa ){ |
75 |
$isa = $isa->name; |
76 |
} |
77 |
} |
78 |
$isa =~ s/\s+\|\s+undef//gi; |
79 |
$out .= "\t\t\t\tisa: Moose.$isa,\n"; |
80 |
} |
81 |
|
82 |
|
83 |
$out .= "\t\t\t},\n"; |
84 |
|
85 |
} |
86 |
|
87 |
$out .= "\t\t},\n"; |
88 |
|
89 |
$out .= "\t}),\n"; |
90 |
|
91 |
$out =~ s/,\n$/\n/; |
92 |
$out .= "});\n"; |
93 |
|
94 |
$out .= "\nconsole.log( 'loaded " . $class->name . " from $filename' );\n"; |
95 |
|
96 |
warn $class->dump(2); |
97 |
|
98 |
warn "get_attribute_list = ",dump( $class->get_attribute_list ); |
99 |
# warn dump( map{ $class->get_attribute($_) } sort $class->get_attribute_list ); |
100 |
|
101 |
warn dump( $class->get_method_list ); |
102 |
|
103 |
print $out; |
104 |
my $path = "static/blib/$filename"; |
105 |
write_file( $path, $out ); |
106 |
warn "# created $path\n"; |
107 |
} |
108 |
|
109 |
=head1 SEE ALSO |
110 |
|
111 |
L<MooseX::AutoDoc> on which this code is based |
112 |
|
113 |
=cut |
114 |
|
115 |
1; |