Permalink
Browse files

Added assets container

  • Loading branch information...
1 parent a8096a6 commit a9e59f036cafea9bf927aa79b919291dd1494072 @vti committed May 3, 2012
Showing with 84 additions and 0 deletions.
  1. +44 −0 lib/Turnaround/AssetsContainer.pm
  2. +40 −0 t/components/AssetsContainerTest.pm
@@ -0,0 +1,44 @@
+package Turnaround::AssetsContainer;
+
+use strict;
+use warnings;
+
+use base 'Turnaround::Base';
+
+sub BUILD {
+ my $self = shift;
+
+ $self->{paths} = [];
+}
+
+sub require {
+ my $self = shift;
+ my ($path, $type) = @_;
+
+ return $self if grep { $path eq $_->{path} } @{$self->{paths}};
+
+ ($type) = $path =~ m/\.([^\.]+)$/ unless $type;
+
+ push @{$self->{paths}}, {type => $type, path => $path};
+
+ return $self;
+}
+
+sub include {
+ my $self = shift;
+ my ($type) = @_;
+
+ my @html;
+ foreach my $asset (@{$self->{paths}}) {
+ next if $type && $asset->{type} ne $type;
+
+ if ($asset->{type} eq 'js') {
+ push @html,
+ qq|<script src="$asset->{path}" type="text/javascript"></script>|;
+ }
+ }
+
+ return join "\n", @html;
+}
+
+1;
@@ -0,0 +1,40 @@
+package AssetsContainerTest;
+
+use strict;
+use warnings;
+
+use base 'TestBase';
+
+use Test::More;
+use Test::Fatal;
+
+use Turnaround::AssetsContainer;
+
+sub include_js : Test {
+ my $self = shift;
+
+ my $assets = $self->_build_assets;
+
+ $assets->require('/foo.js');
+
+ is($assets->include, '<script src="/foo.js" type="text/javascript"></script>');
+}
+
+sub not_add_the_same_path : Test {
+ my $self = shift;
+
+ my $assets = $self->_build_assets;
+
+ $assets->require('/foo.js');
+ $assets->require('/foo.js');
+
+ is($assets->include, '<script src="/foo.js" type="text/javascript"></script>');
+}
+
+sub _build_assets {
+ my $self = shift;
+
+ return Turnaround::AssetsContainer->new(@_);
+}
+
+1;

0 comments on commit a9e59f0

Please sign in to comment.