| 1 | package Articulate::Item; | ||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Articulate::Item - represent an item | ||||||||||
| 9 | |||||||||||
| 10 | =cut | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | Articulate::Item->new( { | ||||||||||
| 15 | meta => {}, | ||||||||||
| 16 | content => 'Hello, World!', | ||||||||||
| 17 | location => 'zone/public/article/hello-world', | ||||||||||
| 18 | } ); | ||||||||||
| 19 | |||||||||||
| 20 | # Construction defaults to item if no better option is available | ||||||||||
| 21 | Articulate::Construction->construct( { ... } ); | ||||||||||
| 22 | |||||||||||
| 23 | An item is a simple storage class for any sort of item which. Items | ||||||||||
| 24 | have metadata, content, and a location. | ||||||||||
| 25 | |||||||||||
| 26 | Although it is acceptable to create items using the C<new> class | ||||||||||
| 27 | method, it is recommended that you construct items using | ||||||||||
| 28 | L<Articulate::Construction>, which will be able to pick an appropriate | ||||||||||
| 29 | subclass of item to construct based on the argument you supply, if you | ||||||||||
| 30 | have configured it to do so. Such a subclass can have semantically | ||||||||||
| 31 | appropriate methods available (see C<_meta_accessor> below for | ||||||||||
| 32 | information on how to create these). | ||||||||||
| 33 | |||||||||||
| 34 | =head1 ATTRIBUTES | ||||||||||
| 35 | |||||||||||
| 36 | =head3 location | ||||||||||
| 37 | |||||||||||
| 38 | Returns the location of the item, as a location object (see | ||||||||||
| 39 | L<Articulate::Location>). Coerces into a location using | ||||||||||
| 40 | C<Articulate::Location::loc>. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | has location => ( | ||||||||||
| 45 | is => 'rw', | ||||||||||
| 46 | 19 | ▶ | default => sub { Articulate::Location->new; }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 49 | ▶ | coerce => sub { Articulate::Location::loc(shift); } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 11 | ); | |||||||||
| 49 | |||||||||||
| 50 | =head3 meta | ||||||||||
| 51 | |||||||||||
| 52 | Returns the item's metadata, as a hashref. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | has meta => ( | ||||||||||
| 57 | is => 'rw', | ||||||||||
| 58 | 24 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 11 | ); | |||||||||
| 60 | |||||||||||
| 61 | =head3 content | ||||||||||
| 62 | |||||||||||
| 63 | Returns the item's content. What it might look like depends entirely on | ||||||||||
| 64 | the content. Typically this is an unblessed scalar value, but it MAY | ||||||||||
| 65 | contain binary data or an L<Articulate::File> object. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | has content => ( | ||||||||||
| 70 | is => 'rw', | ||||||||||
| 71 | 20 | ▶ | default => sub { '' }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 11 | ); | |||||||||
| 73 | |||||||||||
| 74 | =head1 METHOD | ||||||||||
| 75 | |||||||||||
| 76 | =head3 _meta_accessor | ||||||||||
| 77 | |||||||||||
| 78 | # In a subclass of Item | ||||||||||
| 79 | sub author { shift->_meta_accessor('schema/article/author')->(@_) } | ||||||||||
| 80 | |||||||||||
| 81 | # Then, on that subclass | ||||||||||
| 82 | $article->author('user/alice'); | ||||||||||
| 83 | $article->author; | ||||||||||
| 84 | |||||||||||
| 85 | Uses dpath_set or dpath_get from L<Articulate::Syntax> to find or | ||||||||||
| 86 | assign the relevant field in the metadata. | ||||||||||
| 87 | |||||||||||
| 88 | =cut | ||||||||||
| 89 | |||||||||||
| 90 | sub _meta_accessor { | ||||||||||
| 91 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_meta_accessor: not called CC=1 SCAR=6.9
| |||||||||||
| 92 | 0 | my $path = shift; | |||||||||
| 93 | return sub { | ||||||||||
| 94 | 0 | ▶ | if (@_) { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 95 | 0 | dpath_set( $self->meta, $path, @_ ); | |||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | 0 | dpath_get( $self->meta, $path ); | |||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | 0 | } | |||||||||
| 102 | |||||||||||
| 103 | 11 | 1; | |||||||||