| 1 | package Crop::Object::Extern; | ||
| 2 | use base qw / Crop::Object /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Extern | ||
| 6 | Fabrica for various types of links to the extern objects. | ||
| 7 | |||
| 8 | There are 3 types of link to the Extern. | ||
| 9 | |||
| 10 | 1. <Refbook>. | ||
| 11 | Means many-to-one or one-to-one. | ||
| 12 | |||
| 13 | 2. <Bundle> | ||
| 14 | One-to-many produces result in a <Crop::Object::Collection> form. | ||
| 15 | |||
| 16 | 3. <Content> | ||
| 17 | Many-to-many. Result is a <Crop::Object::Collection>. | ||
| 18 | =cut | ||
| 19 | |||
| 20 | use v5.14; | ||
| 21 | use warnings; | ||
| 22 | no warnings 'experimental'; | ||
| 23 | |||
| 24 | use Crop::Error; | ||
| 25 | use Crop::Debug; | ||
| 26 | use Crop::Util 'load_class'; | ||
| 27 | |||
| 28 | =begin nd | ||
| 29 | Variable: our %Attributes | ||
| 30 | Class attributes: | ||
| 31 | |||
| 32 | type - type of link to extern class; could by either a 'bundle', 'content', or 'refbook' | ||
| 33 | |||
| 34 | =cut | ||
| 35 | our %Attributes = ( | ||
| 36 | type => undef, | ||
| 37 | ); | ||
| 38 | |||
| 39 | =begin nd | ||
| 40 | Constructor: new ($src) | ||
| 41 | Fabrica by the 'type' attribute. | ||
| 42 | |||
| 43 | Parameters: | ||
| 44 | $src - raw declaration | ||
| 45 | |||
| 46 | Returns: | ||
| 47 | Exemplar of subclass - if all right | ||
| 48 | undef - in case of any error | ||
| 49 | =cut | ||
| 50 | sub new { | ||
| 51 | my $class = shift; | ||
| 52 | |||
| 53 | return $class->SUPER::new(@_) if $class ne __PACKAGE__; | ||
| 54 | |||
| 55 | my $src = shift; | ||
| 56 | |||
| 57 | exists $src->{type} or return warn 'OBJECT|CRIT: Extern type is not defined'; | ||
| 58 | |||
| 59 | my $subclass; | ||
| 60 | given ($src->{type}) { | ||
| 61 | when ('refbook') { $subclass = 'Refbook' } | ||
| 62 | when ('content') { $subclass = 'Content' } | ||
| 63 | when ('bundle') { $subclass = 'Bundle' } | ||
| 64 | default { return warn "OBJECT|CRIT: No such class for extern worker '$src->{type}'" } | ||
| 65 | } | ||
| 66 | |||
| 67 | my $worker = __PACKAGE__ . "::$subclass"; | ||
| 68 | load_class $worker or return warn "OBJECT|CRIT: Can't load worker for Extern object '$worker'"; | ||
| 69 | |||
| 70 | $worker->new($src); | ||
| 71 | } | ||
| 72 | |||
| 73 | =begin nd | ||
| 74 | Method: make_link ($parent) | ||
| 75 | Create node and link it to the $parent. | ||
| 76 | |||
| 77 | Pure virtual, must be redefined by subclass. | ||
| 78 | |||
| 79 | Returns: | ||
| 80 | an error | ||
| 81 | =cut | ||
| 82 | sub make_link { warn "OBJECT|CRIT: method make_link() must be redefined by subclass" } | ||
| 83 | |||
| 84 | 1; |