Newer Older
127 lines | 2.652kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::Asset;
2
use Mojo::Base 'Mojo::EventEmitter';
3

            
4
use Carp 'croak';
5

            
6
has 'end_range';
7
has start_range => 0;
8

            
9
sub add_chunk { croak 'Method "add_chunk" not implemented by subclass' }
10
sub contains  { croak 'Method "contains" not implemented by subclass' }
11
sub get_chunk { croak 'Method "get_chunk" not implemented by subclass' }
12

            
13
sub is_file {undef}
14

            
15
sub is_range { !!($_[0]->end_range || $_[0]->start_range) }
16

            
17
sub move_to { croak 'Method "move_to" not implemented by subclass' }
18
sub size    { croak 'Method "size" not implemented by subclass' }
19
sub slurp   { croak 'Method "slurp" not implemented by subclass' }
20

            
21
1;
22

            
23
=encoding utf8
24

            
25
=head1 NAME
26

            
27
Mojo::Asset - HTTP content storage base class
28

            
29
=head1 SYNOPSIS
30

            
31
  package Mojo::Asset::MyAsset;
32
  use Mojo::Base 'Mojo::Asset';
33

            
34
  sub add_chunk {...}
35
  sub contains  {...}
36
  sub get_chunk {...}
37
  sub move_to   {...}
38
  sub size      {...}
39
  sub slurp     {...}
40

            
41
=head1 DESCRIPTION
42

            
43
L<Mojo::Asset> is an abstract base class for HTTP content storage.
44

            
45
=head1 EVENTS
46

            
47
L<Mojo::Asset> inherits all events from L<Mojo::EventEmitter>.
48

            
49
=head1 ATTRIBUTES
50

            
51
L<Mojo::Asset> implements the following attributes.
52

            
53
=head2 end_range
54

            
55
  my $end = $asset->end_range;
56
  $asset  = $asset->end_range(8);
57

            
58
Pretend file ends earlier.
59

            
60
=head2 start_range
61

            
62
  my $start = $asset->start_range;
63
  $asset    = $asset->start_range(3);
64

            
65
Pretend file starts later.
66

            
67
=head1 METHODS
68

            
69
L<Mojo::Asset> inherits all methods from L<Mojo::EventEmitter> and implements
70
the following new ones.
71

            
72
=head2 add_chunk
73

            
74
  $asset = $asset->add_chunk('foo bar baz');
75

            
76
Add chunk of data to asset. Meant to be overloaded in a subclass.
77

            
78
=head2 contains
79

            
80
  my $position = $asset->contains('bar');
81

            
82
Check if asset contains a specific string. Meant to be overloaded in a
83
subclass.
84

            
85
=head2 get_chunk
86

            
87
  my $bytes = $asset->get_chunk($offset);
88
  my $bytes = $asset->get_chunk($offset, $max);
89

            
90
Get chunk of data starting from a specific position, defaults to a maximum
91
chunk size of C<131072> bytes. Meant to be overloaded in a subclass.
92

            
93
=head2 is_file
94

            
95
  my $false = $asset->is_file;
96

            
97
False.
98

            
99
=head2 is_range
100

            
101
  my $bool = $asset->is_range;
102

            
103
Check if asset has a L</"start_range"> or L</"end_range">.
104

            
105
=head2 move_to
106

            
107
  $asset = $asset->move_to('/home/sri/foo.txt');
108

            
109
Move asset data into a specific file. Meant to be overloaded in a subclass.
110

            
111
=head2 size
112

            
113
  my $size = $asset->size;
114

            
115
Size of asset data in bytes. Meant to be overloaded in a subclass.
116

            
117
=head2 slurp
118

            
119
  my $bytes = $asset->slurp;
120

            
121
Read all asset data at once. Meant to be overloaded in a subclass.
122

            
123
=head1 SEE ALSO
124

            
125
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
126

            
127
=cut