Skip to content

Commit

Permalink
merged get_data and get_all_data methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 22, 2012
1 parent ec5e315 commit 609ac9e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Changes
Expand Up @@ -6,8 +6,8 @@
- Removed app_class attribute from Mojo::Server.
- Renamed Mojo::CookieJar to Mojo::UserAgent::CookieJar.
- Renamed Mojo::Command to Mojolicious::Command.
- Moved get_all_data and get_data methods from Mojo::Command to
Mojo::Loader.
- Merged get_all_data and get_data methods from Mojo::Command into data
method in Mojo::Loader.
- Moved class_to_file and class_to_path methods from Mojo::Command to
Mojo::Util.
- Updated IO::Socket::SSL requirement to 1.75 for IO::Socket::IP support.
Expand Down
77 changes: 36 additions & 41 deletions lib/Mojo/Loader.pm
Expand Up @@ -10,40 +10,9 @@ use Mojo::Util qw(b64_decode class_to_path);
my %CACHE;

# "Homer no function beer well without."
sub get_all_data {
my ($self, $class) = @_;

# Refresh or use cached data
my $d = do { no strict 'refs'; \*{"$class\::DATA"} };
return $CACHE{$class} || {} unless fileno $d;
seek $d, 0, 0;
my $content = join '', <$d>;
close $d;

# Ignore everything before __DATA__ (windows will seek to start of file)
$content =~ s/^.*\n__DATA__\r?\n/\n/s;

# Ignore everything after __END__
$content =~ s/\n__END__\r?\n.*$/\n/s;

# Split
my @data = split /^@@\s*(.+?)\s*\r?\n/m, $content;
shift @data;
# Find data
my $all = $CACHE{$class} = {};
while (@data) {
my ($name, $content) = splice @data, 0, 2;
$content = b64_decode $content if $name =~ s/\s*\(\s*base64\s*\)$//;
$all->{$name} = $content;
}

return $all;
}

sub get_data {
sub data {
my ($self, $class, $data) = @_;
return $class ? $self->get_all_data($class)->{$data} : undef;
return $class ? $data ? _all($class)->{$data} : _all($class) : undef;
}

# "Olive oil? Asparagus? If your mother wasn't so fancy,
Expand Down Expand Up @@ -92,6 +61,37 @@ sub search {
return \@modules;
}

sub _all {
my $class = shift;

# Refresh or use cached data
my $d = do { no strict 'refs'; \*{"$class\::DATA"} };
return $CACHE{$class} || {} unless fileno $d;
seek $d, 0, 0;
my $content = join '', <$d>;
close $d;

# Ignore everything before __DATA__ (windows will seek to start of file)
$content =~ s/^.*\n__DATA__\r?\n/\n/s;

# Ignore everything after __END__
$content =~ s/\n__END__\r?\n.*$/\n/s;

# Split
my @data = split /^@@\s*(.+?)\s*\r?\n/m, $content;
shift @data;
# Find data
my $all = $CACHE{$class} = {};
while (@data) {
my ($name, $content) = splice @data, 0, 2;
$content = b64_decode $content if $name =~ s/\s*\(\s*base64\s*\)$//;
$all->{$name} = $content;
}

return $all;
}

1;

=head1 NAME
Expand Down Expand Up @@ -120,15 +120,10 @@ L<Mojo::Loader> is a class loader and plugin framework.
L<Mojo::Loader> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 C<get_all_data>
my $all = $loader->get_all_data('Foo::Bar');
Extract all embedded files from the C<DATA> section of a class.
=head2 C<get_data>
=head2 C<data>
my $data = $loader->get_data('Foo::Bar', 'foo_bar');
my $all = $loader->data('Foo::Bar');
my $index = $loader->data('Foo::Bar', 'index.html');
Extract embedded file from the C<DATA> section of a class.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command.pm
Expand Up @@ -60,7 +60,7 @@ sub rel_file { catfile(getcwd(), split /\//, pop) }

sub render_data {
my $self = shift;
Mojo::Template->new->render(Mojo::Loader->get_data(ref $self, shift), @_);
Mojo::Template->new->render(Mojo::Loader->data(ref $self, shift), @_);
}

sub render_to_file {
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -65,12 +65,12 @@ sub get_data_template {
unless ($self->{index}) {
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{Mojo::Loader->get_all_data($class)};
$index->{$_} = $class for keys %{Mojo::Loader->data($class)};
}
}

# Find template
return Mojo::Loader->get_data($self->{index}{$template}, $template);
return Mojo::Loader->data($self->{index}{$template}, $template);
}

sub render {
Expand Down Expand Up @@ -189,7 +189,7 @@ sub _detect_handler {
# DATA templates
unless ($self->{data}) {
my @templates
= map { sort keys %{Mojo::Loader->get_all_data($_)} } @{$self->classes};
= map { sort keys %{Mojo::Loader->data($_)} } @{$self->classes};
s/\.(\w+)$// and $self->{data}{$_} ||= $1 for @templates;
}
return $self->{data}{$file} if exists $self->{data}{$file};
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Static.pm
Expand Up @@ -124,12 +124,12 @@ sub _get_data_file {
unless ($self->{index}) {
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{Mojo::Loader->get_all_data($class)};
$index->{$_} = $class for keys %{Mojo::Loader->data($class)};
}
}

# Find file
return Mojo::Loader->get_data($self->{index}{$rel}, $rel);
return Mojo::Loader->data($self->{index}{$rel}, $rel);
}

sub _get_file {
Expand Down
24 changes: 12 additions & 12 deletions t/mojo/loader.t
Expand Up @@ -86,11 +86,11 @@ my $unix = "@@ template1\nFirst Template\n@@ template2\r\nSecond Template\n";
open my $data, '<', \$unix;
no strict 'refs';
*{"Example::Package::UNIX::DATA"} = $data;
is $loader->get_data('Example::Package::UNIX', 'template1'),
"First Template\n", 'right template';
is $loader->get_data('Example::Package::UNIX', 'template2'),
"Second Template\n", 'right template';
is_deeply [sort keys %{$loader->get_all_data('Example::Package::UNIX')}],
is $loader->data('Example::Package::UNIX', 'template1'), "First Template\n",
'right template';
is $loader->data('Example::Package::UNIX', 'template2'), "Second Template\n",
'right template';
is_deeply [sort keys %{$loader->data('Example::Package::UNIX')}],
[qw(template1 template2)], 'right DATA files';
close $data;

Expand All @@ -100,11 +100,11 @@ my $windows
open $data, '<', \$windows;
no strict 'refs';
*{"Example::Package::Windows::DATA"} = $data;
is $loader->get_data('Example::Package::Windows', 'template3'),
is $loader->data('Example::Package::Windows', 'template3'),
"Third Template\r\n", 'right template';
is $loader->get_data('Example::Package::Windows', 'template4'),
is $loader->data('Example::Package::Windows', 'template4'),
"Fourth Template\r\n", 'right template';
is_deeply [sort keys %{$loader->get_all_data('Example::Package::Windows')}],
is_deeply [sort keys %{$loader->data('Example::Package::Windows')}],
[qw(template3 template4)], 'right DATA files';
close $data;

Expand All @@ -113,12 +113,12 @@ my $mixed = "@\@template5\n5\n\n@@ template6\n6\n@@ template7\n7";
open $data, '<', \$mixed;
no strict 'refs';
*{"Example::Package::Mixed::DATA"} = $data;
is $loader->get_data('Example::Package::Mixed', 'template5'), "5\n\n",
is $loader->data('Example::Package::Mixed', 'template5'), "5\n\n",
'right template';
is $loader->get_data('Example::Package::Mixed', 'template6'), "6\n",
is $loader->data('Example::Package::Mixed', 'template6'), "6\n",
'right template';
is $loader->get_data('Example::Package::Mixed', 'template7'), '7',
is $loader->data('Example::Package::Mixed', 'template7'), '7',
'right template';
is_deeply [sort keys %{$loader->get_all_data('Example::Package::Mixed')}],
is_deeply [sort keys %{$loader->data('Example::Package::Mixed')}],
[qw(template5 template6 template7)], 'right DATA files';
close $data;

0 comments on commit 609ac9e

Please sign in to comment.