File Coverage

blib/lib/Articulate/Construction.pm
Criterion Covered Total %
statement 22 23 95.6
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Articulate::Construction;
2 4     4   3355 use strict;
  4         8  
  4         175  
3 4     4   24 use warnings;
  4         7  
  4         127  
4              
5 4     4   19 use Moo;
  4         6  
  4         28  
6 4     4   1231 use Articulate::Syntax qw(instantiate_array);
  4         8  
  4         31  
7 4     4   1075 use Articulate::Item;
  4         10  
  4         821  
8              
9             with 'Articulate::Role::Component';
10              
11             =head1 NAME
12              
13             Articulate::Construction - create appropriate content item objects given location, meta, content.
14              
15             =cut
16              
17             =head1 CONFIGURATION
18              
19             plugins:
20             Articulate::Construction:
21             constructors:
22             - Articulate::Construction::LocationBased
23              
24             =head1 ATTRIBUTE
25              
26             head3 constructors
27              
28             A list of classes which can be used to construct items.
29              
30             =cut
31              
32             has constructors => (
33             is => 'rw',
34             default => sub { [] },
35             coerce => sub { instantiate_array(@_) }
36             );
37              
38             =head1 METHODS
39              
40             =head3 construct
41              
42             my $item = $construction->construct( {
43             location => $location,
44             meta => $meta,
45             content => $content,
46             } );
47              
48             Iterates through the C and asks each to C an item with the construction data.
49             If no constructor returns a defined vaue, then performs C<< Articulate::Item->new( $args ) >>.
50              
51             Note that of these three pieces of data, it is not guaranteed that all will be available at the time of construction, particularly on inbound communication (as opposed to when retrieving from storage). This is largely dependant on the Service. Location should always be available. Content is often not available.
52              
53             =cut
54              
55             sub construct {
56 8     8 1 776 my $self = shift;
57 8         16 my $args = shift;
58 8         12 my $constructed;
59 8         11 foreach my $constructor ( @{ $self->constructors } ) {
  8         157  
60 8         1346 $constructed = $constructor->construct($args);
61 8 50       436 return $constructed if defined $constructed;
62             }
63 0           return Articulate::Item->new($args);
64             }
65              
66             1;