File Coverage

blib/lib/Web/AssetLib/InputEngine.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 10 50.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 35 40 87.5


line stmt bran cond sub pod time code
1             package Web::AssetLib::InputEngine;
2              
3 9     9   2835244 use Method::Signatures;
  9         48547  
  9         59  
4 9     9   3437 use Moose;
  9         279772  
  9         63  
5              
6             with 'Web::AssetLib::Role::Logger';
7              
8             has 'asset_cache' => (
9             is => 'rw',
10             isa => 'HashRef',
11             default => sub { {} },
12             traits => [qw/Hash/],
13             handles => {
14             addAssetToCache => 'set',
15             getAssetFromCache => 'get'
16             }
17             );
18              
19 9 50   9   157141 method storeAssetContents (:$asset!,:$digest!,:$contents!) {
  10 50   10   19  
  10 50       61  
  10 50       37  
  10 50       18  
  10         37  
  10         28  
  10         20  
  10         17  
  10         29  
  10         20  
  10         14  
  10         34  
  10         31  
20 10         367 $asset->set_digest($digest);
21 10         342 $asset->set_contents($contents);
22             }
23              
24 9     9   1642 no Moose;
  9         13  
  9         41  
25             1;
26              
27             =pod
28            
29             =encoding UTF-8
30            
31             =head1 NAME
32              
33             Web::AssetLib::InputEngine - a base class for writing your own Input Engine
34              
35             =head1 SYNOPSIS
36              
37             package My::Library::InputEngine;
38              
39             use Moose;
40              
41             extends 'Web::AssetLib::InputEngine';
42              
43             sub load {
44             my ( $self, $asset ) = @_;
45              
46             # your special file handing for $asset
47              
48             # store the digest and contents with the asset
49             $self->storeAssetContents(
50             asset => $asset,
51             digest => $digest,
52             contents => $contents
53             );
54             }
55              
56             Using the cache:
57              
58             sub load {
59             ...
60              
61             unless( $self->getAssetFromCache($digest) ){
62             # not in cache, so load it
63              
64             my $contents = ...;
65              
66             # add it to the cache
67             $self->addAssetToCache( $digest => $contents );
68             }
69            
70             ...
71             }
72              
73             =head1 USAGE
74              
75             If you have a need for a special file input scenario, you can simply extend this
76             class, and it will plug in to the rest of the Web::AssetLib pipeline.
77              
78             The only requirement is that your Input Engine implements the
79             L<< load( $asset )|/"load( $asset )" >> method. Load your file however you wish,
80             and then call L<storeAssetContents>.
81              
82             Optionally, you may utilize the cache, with the
83             L<addAssetToCache> and L<getAssetFromCache> methods.
84              
85             =head1 IMPLEMENTATION
86              
87             =head2 load( $asset )
88              
89             Load/consume an asset represented by C<< $asset >>, which will be a
90             L<Web::AssetLib::Asset> object. Load however you'd like, then call
91             L<storeAssetContents> to store the file for later use in the pipeline.
92              
93             =head1 METHODS
94              
95             =head2 storeAssetContents
96              
97             $engine->storeAssetContents(
98             asset => $asset,
99             digest => $digest,
100             contents => $contents
101             );
102              
103             Associates the file contents and digest with the asset instance. C<< $asset >>
104             should be a L<Web::AssetLib::Asset> object, and C<< $contents >> and C<< $digest >>
105             must be strings.
106              
107             This is the only method that must be called when you implement the
108             L<< load()|/"load( $asset )" >> method.
109              
110             All arguments are required.
111              
112             =head2 addAssetToCache
113              
114             $engine->addAssetToCache(
115             digest => $digest
116             );
117            
118             Creates a mapping between your file C<< $digest >> and file C<< $contents >> in the
119             cache. It is reccomended that the cache be utilized when implementing L<< load()|/"load( $asset )" >>
120             but it is not a requirement.
121              
122             =head2 getAssetFromCache
123              
124             my $asset = $engine->getAssetFromCache( $digest );
125            
126             Returns file contents associated with the digest if present in cache, otherwise
127             returns undef. It is reccomended that the cache be utilized when
128             implementing L<< load()|/"load( $asset )" >> but it is not a requirement.
129              
130             =head1 SEE ALSO
131              
132             L<Web::AssetLib::InputEngine::LocalFile>
133              
134             L<Web::AssetLib::InputEngine::RemoteFile>
135              
136             L<Web::AssetLib::InputEngine::Content>
137              
138             =head1 AUTHOR
139            
140             Ryan Lang <rlang@cpan.org>
141              
142             =cut