Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add image class to hold refs to images
The Statocles::Image class holds a reference to an image for a document so that they can be used by other templates. For example, the blog index could show a thumbnail image or a banner image in the blog list view. Refs #409
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package Statocles::Image; | ||
# ABSTRACT: A reference to an image | ||
|
||
=head1 SYNOPSIS | ||
my $img = Statocles::Image->new( | ||
src => '/path/to/image.jpg', | ||
alt => 'Alternative text', | ||
); | ||
=head1 DESCRIPTION | ||
This class holds a link to an image, and the attributes required to | ||
render its markup. This is used by L<documents|Statocles::Document/images> | ||
to associate images with the content. | ||
=cut | ||
|
||
use Statocles::Base 'Class'; | ||
use Scalar::Util qw( blessed ); | ||
|
||
=attr src | ||
The source URL of the image. Required. | ||
=cut | ||
|
||
has src => ( | ||
is => 'rw', | ||
isa => Str, | ||
required => 1, | ||
coerce => sub { | ||
my ( $href ) = @_; | ||
if ( blessed $href && $href->isa( 'Path::Tiny' ) ) { | ||
return $href->stringify; | ||
} | ||
return $href; | ||
}, | ||
); | ||
|
||
=attr alt | ||
The text to display if the image cannot be fetched or rendered. This is also | ||
the text to use for non-visual media. | ||
If missing, the image is presentational only, not content. | ||
=cut | ||
|
||
has alt => ( | ||
is => 'rw', | ||
isa => Str, | ||
default => sub { '' }, | ||
); | ||
|
||
=attr width | ||
The width of the image, in pixels. | ||
=cut | ||
|
||
has width => ( | ||
is => 'rw', | ||
isa => Int, | ||
); | ||
|
||
=attr height | ||
The height of the image, in pixels. | ||
=cut | ||
|
||
has height => ( | ||
is => 'rw', | ||
isa => Int, | ||
); | ||
|
||
=attr role | ||
The L<ARIA|http://www.w3.org/TR/wai-aria/> role for this image. If the L</alt> | ||
attribute is empty, this attribute defaults to C<"presentation">. | ||
=cut | ||
|
||
has role => ( | ||
is => 'rw', | ||
isa => Maybe[Str], | ||
lazy => 1, | ||
default => sub { | ||
return !$_[0]->alt ? 'presentation' : undef; | ||
}, | ||
); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
use Statocles::Base 'Test'; | ||
use Statocles::Image; | ||
|
||
subtest 'constructor' => sub { | ||
my %required = ( | ||
src => '/images/test.jpg', | ||
); | ||
|
||
test_constructor( | ||
'Statocles::Image', | ||
required => \%required, | ||
default => { | ||
role => 'presentation', | ||
}, | ||
); | ||
|
||
subtest 'coerce' => sub { | ||
|
||
subtest 'Path object' => sub { | ||
my $img; | ||
lives_ok { | ||
$img = Statocles::Image->new( | ||
src => Path::Tiny->new( 'images', 'test.jpg' ), | ||
); | ||
} or return; | ||
is $img->src, 'images/test.jpg'; | ||
}; | ||
}; | ||
|
||
subtest 'default role' => sub { | ||
my $img = Statocles::Image->new( | ||
src => 'images/test.jpg', | ||
alt => 'Test alt', | ||
); | ||
is $img->role, undef, 'no default if alt set'; | ||
}; | ||
}; | ||
|
||
done_testing; |