Skip to content

Commit

Permalink
add image class to hold refs to images
Browse files Browse the repository at this point in the history
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
preaction committed Dec 6, 2015
1 parent 6cb8fce commit 2d81fd5
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
94 changes: 94 additions & 0 deletions lib/Statocles/Image.pm
@@ -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;
40 changes: 40 additions & 0 deletions t/image.t
@@ -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;

0 comments on commit 2d81fd5

Please sign in to comment.