Skip to content

Commit

Permalink
mapping is a better name than types
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 25, 2015
1 parent e52cc85 commit cc883b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -24,6 +24,7 @@
Mojolicious::Controller.
- Removed deprecated keep_alive_requests setting from Hypnotoad.
- Renamed pattern attribute in Mojolicious::Routes::Route to unparsed.
- Renamed types attribute in Mojolicious::Types to mapping.
- Renamed template attribute in Mojo::Template to unparsed.
- Renamed all_contents, contents, following_siblings, next_sibling,
preceding_siblings and previous_sibling methods in Mojo::DOM to
Expand Down
20 changes: 10 additions & 10 deletions lib/Mojolicious/Types.pm
@@ -1,7 +1,7 @@
package Mojolicious::Types;
use Mojo::Base -base;

has types => sub {
has mapping => sub {
{
appcache => ['text/cache-manifest'],
atom => ['application/atom+xml'],
Expand Down Expand Up @@ -45,18 +45,18 @@ sub detect {

# Detect extensions from MIME types
my %reverse;
my $types = $self->types;
for my $ext (sort keys %$types) {
my @types = @{$types->{$ext}};
my $mapping = $self->mapping;
for my $ext (sort keys %$mapping) {
my @types = @{$mapping->{$ext}};
push @{$reverse{$_}}, $ext for map { s/\;.*$//; lc $_ } @types;
}
return [map { @{$reverse{$_} // []} } @detected];
}

sub type {
my ($self, $ext, $type) = @_;
return $self->types->{lc $ext}[0] unless $type;
$self->types->{lc $ext} = ref $type ? $type : [$type];
return $self->mapping->{lc $ext}[0] unless $type;
$self->mapping->{lc $ext} = ref $type ? $type : [$type];
return $self;
}

Expand Down Expand Up @@ -113,12 +113,12 @@ The most common ones are already defined.
L<Mojolicious::Types> implements the following attributes.
=head2 types
=head2 mapping
my $map = $types->types;
$types = $types->types({png => ['image/png']});
my $mapping = $types->mapping;
$types = $types->mapping({png => ['image/png']});
List of MIME types.
MIME type mapping.
=head1 METHODS
Expand Down
10 changes: 5 additions & 5 deletions t/mojolicious/types.t
Expand Up @@ -54,11 +54,11 @@ is_deeply $t->detect('TEXT/HTML;Q=0.8,*/*;Q=0.9'), [], 'no formats';

# Alternatives
$t->type(json => ['application/json', 'text/x-json']);
is $t->types->{json}[0], 'application/json', 'right type';
is $t->types->{json}[1], 'text/x-json', 'right type';
ok !$t->types->{json}[2], 'no type';
is_deeply $t->types->{htm}, ['text/html'], 'right type';
is_deeply $t->types->{html}, ['text/html;charset=UTF-8'], 'right type';
is $t->mapping->{json}[0], 'application/json', 'right type';
is $t->mapping->{json}[1], 'text/x-json', 'right type';
ok !$t->mapping->{json}[2], 'no type';
is_deeply $t->mapping->{htm}, ['text/html'], 'right type';
is_deeply $t->mapping->{html}, ['text/html;charset=UTF-8'], 'right type';
is_deeply $t->detect('application/json'), ['json'], 'right formats';
is_deeply $t->detect('text/x-json'), ['json'], 'right formats';
is_deeply $t->detect('TEXT/X-JSON;q=0.1'), ['json'], 'right formats';
Expand Down

0 comments on commit cc883b8

Please sign in to comment.