| 1 | package JavaScript::Librarian; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | JavaScript::Librarian - Load and use libraries of JavaScript packages | ||||||||||
| 8 | |||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||
| 10 | |||||||||||
| 11 | C<JavaScript::Librarian> is a package for loading and using "libraries" | ||||||||||
| 12 | of JavaScript packages, managing dependencies between the files, and | ||||||||||
| 13 | generating fragments of HTML with the E<lt>scriptE<gt> tags to load them | ||||||||||
| 14 | in the correct order. | ||||||||||
| 15 | |||||||||||
| 16 | =head1 STATUS | ||||||||||
| 17 | |||||||||||
| 18 | This is an early release, and while it implements the core object and | ||||||||||
| 19 | logic, this package does not yet come with any | ||||||||||
| 20 | L<Library|JavaScript::Librarian::Library> sub-classes capable of loading | ||||||||||
| 21 | the required metadata from anything. | ||||||||||
| 22 | |||||||||||
| 23 | This will be dealth with in a seperate package, or in a future version | ||||||||||
| 24 | of this one. For the moment consider it something you can use to build | ||||||||||
| 25 | your own modules. See the source code for more documentation. | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 1 | ▶ | use URI (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | use Clone (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | ▶ | use File::Spec::Unix (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 1 | ▶ | use Algorithm::Dependency::Ordered (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | ▶ | use JavaScript::Librarian::Book (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | ▶ | use JavaScript::Librarian::Library (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 1 | ▶ | use Params::Coerce '_URI' => 'URI'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 1 | ▶ | use Params::Coerce '_Library' => 'JavaScript::Librarian::Library'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use vars qw{$VERSION}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 1 | $VERSION = '1.00'; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | ##################################################################### | ||||||||||
| 49 | # Constructor and Accessors | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||
| 52 | 0 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | my %args = @_; | |||||||||
| 54 | 0 | ▶ | my $base = $class->_URI($args{base}) or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | ▶ | my $library = $class->_Library($args{library}) or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | # Create the dependency resolver | ||||||||||
| 58 | 0 | ▶ | my $resolver = Algorithm::Dependency::Ordered->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | source => $library, | ||||||||||
| 60 | ignore_orphans => 1, | ||||||||||
| 61 | ) or return undef; | ||||||||||
| 62 | |||||||||||
| 63 | # Create the basic object | ||||||||||
| 64 | 0 | my $self = bless { | |||||||||
| 65 | base => $base, | ||||||||||
| 66 | library => $library, | ||||||||||
| 67 | resolver => $resolver, | ||||||||||
| 68 | selected => {}, | ||||||||||
| 69 | }, $class; | ||||||||||
| 70 | |||||||||||
| 71 | # Add any packages to select passed to the constructor | ||||||||||
| 72 | 0 | ▶ | if ( ref $args{select} eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | foreach my $book ( @{$args{select}} ) { | |||||||||
| 74 | 0 | ▶ | $self->select( $_ ) or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | $self; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub base { | ||||||||
|
Subroutine
base: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 82 | 0 | Clone::clone $_[0]->{base}; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub library { | ||||||||
|
Subroutine
library: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 86 | 0 | $_[0]->{library}; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | ▶ | sub resolver { | ||||||||
|
Subroutine
resolver: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 90 | 0 | $_[0]->{resolver}; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | ##################################################################### | ||||||||||
| 99 | # Main Methods | ||||||||||
| 100 | |||||||||||
| 101 | # Select a package we need | ||||||||||
| 102 | 0 | ▶ | sub add { | ||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 103 | 0 | my $self = shift; | |||||||||
| 104 | 0 | ▶ | my $book = $self->library->item($_[0]) ? shift : return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | $self->{selected}->{$book} = 1; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | # Find the schedule for the currently selected items | ||||||||||
| 109 | 0 | ▶ | sub schedule { | ||||||||
|
Subroutine
schedule: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 110 | 0 | my $self = shift; | |||||||||
| 111 | 0 | $self->resolver->schedule( sort keys %{$self->{selected}} ); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | # Get the list of paths of JavaScript files to load | ||||||||||
| 115 | 0 | ▶ | sub paths { | ||||||||
|
Subroutine
paths: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 116 | 0 | my $self = shift; | |||||||||
| 117 | 0 | ▶ | my $schedule = $self->schedule or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | my $library = $self->library; | |||||||||
| 119 | |||||||||||
| 120 | # Map to file names | ||||||||||
| 121 | 0 | my @paths = map { $library->item($_)->path } @$schedule; | |||||||||
| 122 | |||||||||||
| 123 | # Move them under the base URI | ||||||||||
| 124 | 0 | @paths = map { $self->_path_URI($_) } @paths; | |||||||||
| 125 | |||||||||||
| 126 | 0 | \@paths; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | # Generate a URI relative to the base, but without the assumption that | ||||||||||
| 130 | # the base URI is absolute. | ||||||||||
| 131 | 0 | ▶ | sub _path_URI { | ||||||||
|
Subroutine
_path_URI: not called CC=1 SCAR=6.9
| |||||||||||
| 132 | 0 | my $self = shift; | |||||||||
| 133 | 0 | my $URI = $self->base; | |||||||||
| 134 | 0 | my $path = File::Spec::Unix->catfile( $URI->path, @_ ); | |||||||||
| 135 | 0 | $URI->path( $path ); | |||||||||
| 136 | 0 | $URI; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | # Generates a string of HTML to load the books | ||||||||||
| 140 | 0 | ▶ | sub html { | ||||||||
|
Subroutine
html: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 141 | 0 | my $self = shift; | |||||||||
| 142 | 0 | ▶ | my $paths = $self->paths or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | join "\n", map { | ||||||||||
| 144 | 0 | qq~<script language="JavaScript" src="$_" type="text/javascript"></script>~ | |||||||||
| 145 | } @$paths; | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | # XHTML version of the above...? | ||||||||||
| 149 | 0 | ▶ | sub xhtml { | ||||||||
|
Subroutine
xhtml: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 150 | 0 | ''; ### FIXME - Finish this | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 1 | 1; | |||||||||
| 154 | |||||||||||
| 155 | =pod | ||||||||||
| 156 | |||||||||||
| 157 | =head1 SUPPORT | ||||||||||
| 158 | |||||||||||
| 159 | Bugs should always be submitted via the CPAN bug tracker | ||||||||||
| 160 | |||||||||||
| 161 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JavaScript-Librarian> | ||||||||||
| 162 | |||||||||||
| 163 | For other issues, contact the maintainer | ||||||||||
| 164 | |||||||||||
| 165 | =head1 AUTHORS | ||||||||||
| 166 | |||||||||||
| 167 | Adam Kennedy E<lt>cpan@ali.asE<gt>, L<http://ali.as/> | ||||||||||
| 168 | |||||||||||
| 169 | =head1 COPYRIGHT | ||||||||||
| 170 | |||||||||||
| 171 | Copyright (c) 2005 Adam Kennedy. All rights reserved. | ||||||||||
| 172 | This program is free software; you can redistribute | ||||||||||
| 173 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 174 | |||||||||||
| 175 | The full text of the license can be found in the | ||||||||||
| 176 | LICENSE file included with this module. | ||||||||||
| 177 | |||||||||||
| 178 | =cut | ||||||||||