
theory at bricolage
Mar 6, 2009, 3:25 PM
Post #1 of 1
(426 views)
Permalink
|
|
[8458] Sugar for mode checking in templates.
|
|
Revision: 8458 Author: theory Date: 2009-03-06 15:25:48 -0800 (Fri, 06 Mar 2009) ViewCVS: http://viewsvn.bricolage.cc/?rev=8458&view=rev Log Message: ----------- Sugar for mode checking in templates. Modified Paths: -------------- bricolage/trunk/lib/Bric/Changes.pod bricolage/trunk/lib/Bric/Util/Burner.pm bricolage/trunk/t/Bric/Util/Burner/DevTest.pm Modified: bricolage/trunk/lib/Bric/Changes.pod =================================================================== --- bricolage/trunk/lib/Bric/Changes.pod 2009-03-06 11:53:18 UTC (rev 8457) +++ bricolage/trunk/lib/Bric/Changes.pod 2009-03-06 23:25:48 UTC (rev 8458) @@ -86,6 +86,12 @@ C<< Bric::Util::ApacheReq->url >>. This removes a fair bit of duplicate code found here and there. [David] +=item * + +Added the C<publishing()>, C<previewing()>, and C<compling()> sugar methods to +L<Bric::Util::Burner|Bric::Util::Burner> because I'm sick of seeing the same +code to check modes in templates all the time. [David] + =back =head2 Bug Fixes Modified: bricolage/trunk/lib/Bric/Util/Burner.pm =================================================================== --- bricolage/trunk/lib/Bric/Util/Burner.pm 2009-03-06 11:53:18 UTC (rev 8457) +++ bricolage/trunk/lib/Bric/Util/Burner.pm 2009-03-06 23:25:48 UTC (rev 8458) @@ -194,8 +194,10 @@ use base qw(Bric Exporter); our @EXPORT_OK = qw(PUBLISH_MODE PREVIEW_MODE SYNTAX_MODE); -our %EXPORT_TAGS = ( all => \@EXPORT_OK, - modes => \@EXPORT_OK); +our %EXPORT_TAGS = ( + all => \@EXPORT_OK, + modes => \@EXPORT_OK +); #==============================================================================# # Dependencies # @@ -235,7 +237,7 @@ #======================================# use constant PUBLISH_MODE => 1; use constant PREVIEW_MODE => 2; -use constant SYNTAX_MODE => 3; +use constant SYNTAX_MODE => 3; #==============================================================================# # Fields # @@ -593,6 +595,21 @@ B<Notes:> NONE. +=item my $publishing = $burner->publishing + +=item my $previewing = $burner->previewing + +=item my $compiling = $burner->compiling + +Returns true if the burner is currently in publish, preview, or syntax mode, +respectively. Really it's just sugar for checking the mode directly. + +=cut + +sub previewing { (shift->get_mode || 0) == PREVIEW_MODE } +sub publishing { (shift->get_mode || 0) == PUBLISH_MODE } +sub compiling { (shift->get_mode || 0) == SYNTAX_MODE } + =item my $encoding = $burner->get_encoding Returns the character set encoding to be used to write out the contents of a Modified: bricolage/trunk/t/Bric/Util/Burner/DevTest.pm =================================================================== --- bricolage/trunk/t/Bric/Util/Burner/DevTest.pm 2009-03-06 11:53:18 UTC (rev 8457) +++ bricolage/trunk/t/Bric/Util/Burner/DevTest.pm 2009-03-06 23:25:48 UTC (rev 8458) @@ -5,7 +5,7 @@ use utf8; use base qw(Bric::Test::DevBase); use Test::More; -use Bric::Util::Burner; +use Bric::Util::Burner qw(:modes); use Bric::Biz::Asset::Template; use Bric::Util::Trans::FS; use Bric::Util::Time qw(strfdate); @@ -810,6 +810,36 @@ unlink $file, $file1, $file2, $prev_file, $prev_file1; } +sub test_modes : Test(10) { + my $self = shift; + + # We'll need something to test. + ok my $burner = Bric::Util::Burner->new, 'Construct a burner'; + + # Set up mocking the mode. + my $mode; + my $mock = Test::MockModule->new(ref $burner); + $mock->mock('get_mode' => sub { $mode }); + + # Mock publish mode. + $mode = PUBLISH_MODE; + ok $burner->publishing, 'Burner should be publishing'; + ok !$burner->previewing, 'It should not be previewing'; + ok !$burner->compiling, 'It should not be compiling'; + + # Mock preview mode. + $mode = PREVIEW_MODE; + ok !$burner->publishing, 'Burner should not be publishing'; + ok $burner->previewing, 'It should be previewing'; + ok !$burner->compiling, 'It should not be compiling'; + + # Mock syntax mode. + $mode = SYNTAX_MODE; + ok !$burner->publishing, 'Burner should not be publishing'; + ok !$burner->previewing, 'It should not be previewing'; + ok $burner->compiling, 'It should be compiling'; +} + ############################################################################## sub burn_cleanup : Test(teardown) { my $self = shift;
|