From: Chris W. <la...@us...> - 2005-02-08 01:06:51
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2 In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18645/lib/OpenInteract2 Modified Files: Brick.pm Log Message: OIN-121: add list_bricks(); move the template to generate new bricks into this class Index: Brick.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Brick.pm,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Brick.pm 2 Feb 2005 13:16:26 -0000 1.2 --- Brick.pm 8 Feb 2005 01:06:40 -0000 1.3 *************** *** 6,9 **** --- 6,10 ---- use base qw( Class::Factory ); use File::Basename qw( basename dirname ); + use File::Path qw( mkpath ); use File::Spec::Functions qw( catfile ); use Log::Log4perl qw( get_logger ); *************** *** 20,23 **** --- 21,29 ---- my ( $log ); + sub list_bricks { + my ( $class ) = @_; + return $class->get_registered_types; + } + sub list_resources { my ( $self ) = @_; *************** *** 64,68 **** NAME: foreach my $name ( @resource_names ) { ! my $info = $self->load_resource( $name ); my ( $final_dest_spec ); $TEMPLATE->process( \$info->{destination}, $template_vars, \$final_dest_spec ) --- 70,76 ---- NAME: foreach my $name ( @resource_names ) { ! my $info = $self->load_resource( $name ); ! ! # First process any template keys in the destination... my ( $final_dest_spec ); $TEMPLATE->process( \$info->{destination}, $template_vars, \$final_dest_spec ) *************** *** 83,86 **** --- 91,95 ---- } + # ...next, evaluate the content if we're supposed to my $content = $info->{content}; unless ( 'no' eq lc $info->{evaluate} ) { *************** *** 101,104 **** --- 110,117 ---- } + my $dest_dir = dirname( $full_dest_file ); + unless ( -d $dest_dir ) { + mkpath( $dest_dir ); + } open( OUT, '>', $full_dest_file ) || oi_error "Cannot write resource '$name' to '$full_dest_file': $!"; *************** *** 155,158 **** --- 168,267 ---- ); + ######################################## + # GENERATING NEW BRICKS + + sub get_brick_class_template { + return <<'TEMPLATE'; + package OpenInteract2::Brick::[% brick_name %]; + + use strict; + use base qw( OpenInteract2::Brick ); + use OpenInteract2::Exception; + + my %INLINED_SUBS = ( + [% FOREACH file_info = all_files -%] + '[% file_info.name %]' => '[% file_info.inline_name %]', + [% END -%] + ); + + sub get_name { + return '[% lc_brick_name %]'; + } + + sub get_resources { + return ( + [% FOREACH file_info = all_files -%] + '[% file_info.name %]' => [ '[% file_info.destination %]', '[% file_info.evaluate %]' ], + [% END -%] + ); + } + + sub load { + my ( $self, $resource_name ) = @_; + my $inline_sub_name = $INLINED_SUBS{ $resource_name }; + unless ( $inline_sub_name ) { + OpenInteract2::Exception->throw( + "Resource name '$resource_name' not found ", + "in ", ref( $self ), "; cannot load content." ); + } + return $self->$inline_sub_name(); + } + + OpenInteract2::Brick->register_factory_type( get_name() => __PACKAGE__ ); + + =pod + + =head1 NAME + + OpenInteract2::Brick::[% brick_name %] - [% brick_summary %] + + =head1 SYNOPSIS + + [% brick_example | indent(2) %] + + =head1 DESCRIPTION + + [% brick_description %] + + =head2 Resources + + You can grab resources individually using the names below and + C<load_resource()> and C<copy_resources_to()>, or you can copy all the + resources at once using C<copy_all_resources_to()> -- see + L<OpenInteract2::Brick> for details. + + =over 4 + + [% FOREACH file_info = all_files %] + =item B<[% file_info.name %]> + [% END %] + + =back + + =head1 COPYRIGHT + + Copyright (c) 2005 [% author_names.join( ', ' ) %]. All rights reserved. + + This library is free software; you can redistribute it and/or modify + it under the same terms as Perl itself. + + =head1 AUTHORS + + [% FOREACH author_info = authors %] + [% author_info.name %] E<lt>[% author_info.email %]E<gt> + [% END %] + + =cut + + [% FOREACH file_info = all_files %] + sub [% file_info.inline_name %] { + return <<'SOMELONGSTRING'; + [% file_info.contents %] + SOMELONGSTRING + } + [% END %] + TEMPLATE + } + 1; *************** *** 194,197 **** --- 303,318 ---- should always be a lowercased value. + B<list_bricks()> + + Returns a sorted list of all available brick names. With the name you + can instantiate a new brick: + + my @brick_names = OpenInteract2::Brick->list_bricks; + foreach my $name ( @brick_names ) { + my $brick = OpenInteract2::Brick->new( $name ); + print "Resources in brick '$name': ", + join( ", ", $brick->list_resources ), "\n"; + } + =head1 OBJECT METHODS |