File Coverage

blib/lib/Socialtext/WikiObject/Factory.pm
Criterion Covered Total %
statement 22 22 100.0
branch 4 8 50.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 31 35 88.5


line stmt bran cond sub pod time code
1             package Socialtext::WikiObject::Factory;
2 1     1   691 use strict;
  1         2  
  1         27  
3 1     1   4 use warnings;
  1         2  
  1         26  
4 1     1   3 use Carp qw/croak/;
  1         2  
  1         228  
5              
6             =head1 NAME
7              
8             Socialtext::WikiObject::Factory - Create an approprate WikiObject from Magic Tags
9              
10             =cut
11              
12             our $VERSION = '0.01';
13              
14             =head1 SYNOPSIS
15              
16             # Set a magic tag to define WikiObject subclass
17             $rester->put_page($page_name, $page_text);
18             $rester->put_pagetag($page_name, '.wikiobject=YAML');
19              
20             # Use the factory to create the appropriate class
21             my $wo = Socialtext::WikiObject::Factory->new(
22             rester => $rester,
23             page => $page_name,
24             );
25             isa_ok $wo, 'Socialtext::WikiObject::YAML';
26              
27             =head1 DESCRIPTION
28              
29             Socialtext::WikiObject::Factory reads magic tags on a page, and then
30             creates a WikiObject of the appropriate class, as defined in the magic tag.
31              
32             =head1 FUNCTIONS
33              
34             =head2 new( %opts )
35              
36             Create a new wiki object. Options:
37              
38             =over 4
39              
40             =item rester
41              
42             Users must provide a Socialtext::Resting object setup to use the desired
43             workspace and server.
44              
45             =item page
46              
47             The page to load. Mandatory.
48              
49             =back
50              
51             =cut
52              
53             sub new {
54 2     2 1 25 my (undef, %opts) = @_;
55 2 50       12 croak "rester is mandatory!" unless $opts{rester};
56 2 50       6 croak "page is mandatory!" unless $opts{page};
57              
58 2         5 my $class = 'Socialtext::WikiObject';
59              
60 2         4 my $rester = $opts{rester};
61 2         10 my @tags = $rester->get_pagetags($opts{page});
62 2         7 for my $t (@tags) {
63 1 50       9 if ($t =~ m/^\.wikiobject=(.+)$/) {
64 1         5 $class .= '::' . ucfirst($1);
65 1         4 last;
66             }
67             }
68              
69 2         532 eval "require $class";
70 2 50       200 die if $@;
71 2         28 return $class->new(%opts);
72             }
73              
74             =head1 AUTHOR
75              
76             Luke Closs, C<< >>
77              
78             =head1 BUGS
79              
80             Please report any bugs or feature requests to
81             L.
82             I will be notified, and then you'll automatically be notified of progress on
83             your bug as I make changes.
84              
85             =head1 COPYRIGHT & LICENSE
86              
87             Copyright 2007 Luke Closs, all rights reserved.
88              
89             This program is free software; you can redistribute it and/or modify it
90             under the same terms as Perl itself.
91              
92             =cut
93              
94             1;