Catalyst-Controller-AutoAssets

 view release on metacpan or  search on metacpan

lib/Catalyst/Controller/AutoAssets/Handler/ImageSet.pm  view on Meta::CPAN

    ($name) = reverse split(/\//,$path) if($self->flatten_paths);
    if(exists $manifest->{$name}) {
      $self->_app->log->warn("Not including duplicate filename '$name' ('$path')");
      next;
    }
    # This is being done this way to make it easy for derived classes to put
    # additional data in the manifest associated with each item
    $manifest->{$name} = { subfile => $path };
  }
  
  $self->manifest($manifest);
}
after set_subfile_meta => sub { (shift)->rebuild_manifest };


around asset_request => sub {
  my ( $orig, $self, $c, $sha1, @args ) = @_;
  
  ## Just for debugging:
  #if($sha1 eq 'dump_manifest') {
  #  $self->clear_asset;
  #  $self->prepare_asset;
  #  
  #  use Data::Dumper::Concise 'Dumper';
  #  $c->response->header('Content-Type' => 'text/plain');
  #  $c->response->body( 
  #    $self->asset_name . ': ' . scalar(keys %{$self->manifest}) . " Items\n\n" .
  #    Dumper($self->manifest)
  #  );
  #  return $c->detach;
  #}
  
  # If we wanted both/either the flattened and orig paths to work we could
  # call $self->_unflatten_path(join('/',@args)) instead below
  return $self->$orig($c, $sha1, $self->_unflatten_path($args[0]))
    if ($self->flatten_paths);

  return $self->$orig($c, $sha1, @args);
};

sub _unflatten_path {
  my ($self, $name) = @_;
  return undef unless (defined $name);
  my $itm = $self->manifest->{$name} or return $name;
  return $itm->{subfile};
}

around _subfile_mtime_verify => sub {
  my ($orig, $self, $path) = @_;
  return $self->$orig($self->_unflatten_path($path));
};

around asset_path => sub {
  my ($orig, $self, @subpath) = @_;
  return $self->flatten_paths 
    ?  join('/',$self->base_path,$self->asset_name,@subpath)
    : $self->$orig(@subpath);
};


has '_persist_attrs', is => 'ro', isa => 'ArrayRef', default => sub{[qw(
 built_mtime
 inc_mtimes
 last_fingerprint_calculated
 subfile_meta
 _excluded_paths
 manifest
)]};


sub img_tag {
  my ($self, @path) = @_;
  my $name = join('/',@path);
  return '<img alt="' . $name . 
    '" src="' . $self->asset_path($name) . '">';
}

sub img_tag_title {
  my ($self, @path) = @_;
  my $name = join('/',@path);
  return '<img alt="' . $name . '" title="' . $name . 
    '" src="' . $self->asset_path($name) . '">';
}


sub dump_all_img_tags {
  my $self = shift;
  return join("\n",map { $self->img_tag_title($_) } (sort keys %{$self->manifest}));
}


1;

__END__

=pod

=head1 NAME

Catalyst::Controller::AutoAssets::Handler::ImageSet - ImageSet type handler

=head1 SYNOPSIS

In your controller:

  package MyApp::Controller::Assets::MyImages;
  use parent 'Catalyst::Controller::AutoAssets';
  
  1;

Then, in your .conf:

  <Controller::Assets::MyImages>
    include        root/images/
    type           ImageSet
    flatten_paths  1
    include_regex  '\.(png|jpg|gif)$'
  </Controller::Assets::MyImages>

And in your .tt files:



( run in 0.564 second using v1.01-cache-2.11-cpan-39bf76dae61 )