| 1 | package Crop::Object::Extern::Bundle; | ||
| 2 | use base qw / Crop::Object::Extern /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Extern::Bundle | ||
| 6 | Link one-to-many in 'one' side. | ||
| 7 | |||
| 8 | For example: User is borned in City. Many Users could have one City. | ||
| 9 | Bob & Alice is born in London, so a selected city London has many users (Bob & Alice). | ||
| 10 | |||
| 11 | Is assumed the record in bundle is always collection of examplar. | ||
| 12 | |||
| 13 | (start code) | ||
| 14 | our %Attributes = ( | ||
| 15 | #city name | ||
| 16 | name => undef, | ||
| 17 | users => {type => 'cache'}, | ||
| 18 | EXT => { | ||
| 19 | users => { | ||
| 20 | type => 'bundle', | ||
| 21 | class => 'User', | ||
| 22 | xattr => {id => 'id_city'}, | ||
| 23 | view => 'users', | ||
| 24 | }, | ||
| 25 | }, | ||
| 26 | ); | ||
| 27 | (end code) | ||
| 28 | =cut | ||
| 29 | |||
| 30 | use v5.14; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | use Crop::Error; | ||
| 34 | use Crop::Util 'load_class'; | ||
| 35 | use Crop::Object::Constants; | ||
| 36 | use Crop::Object::Warehouse::Lang::SQL::Query::Select::Node::Bundle; | ||
| 37 | |||
| 38 | use Crop::Debug; | ||
| 39 | |||
| 40 | =begin nd | ||
| 41 | Variable: our %Attributes | ||
| 42 | Class attributes: | ||
| 43 | |||
| 44 | class - class of extended object | ||
| 45 | view - cached attribute where reuslt goes to | ||
| 46 | xattr - hash of corresponding attributes of extern key base=>ext | ||
| 47 | =cut | ||
| 48 | our %Attributes = ( | ||
| 49 | class => {mode => 'read'}, | ||
| 50 | view => {mode => 'read'}, | ||
| 51 | xattr => {mode => 'read'}, | ||
| 52 | ); | ||
| 53 | |||
| 54 | =begin nd | ||
| 55 | Method: make_link ($parent) | ||
| 56 | Create node, then link it to the $parent. | ||
| 57 | |||
| 58 | The 'clause' attribute of new node will not be established. Calling code must do that work. | ||
| 59 | |||
| 60 | Returns the 'content' node since calling code could set the 'clause' attribute for it. And the 'content' | ||
| 61 | attribute is the 'bottom' child of two new nodes. | ||
| 62 | |||
| 63 | Parameters: | ||
| 64 | $parent - parent node | ||
| 65 | |||
| 66 | Returns: | ||
| 67 | new node - if ok | ||
| 68 | undef - otherwise | ||
| 69 | |||
| 70 | =cut | ||
| 71 | sub make_link { | ||
| 72 | my ($self, $parent) = @_; | ||
| 73 | |||
| 74 | my $class = $self->{class}; | ||
| 75 | load_class($class) or return warn "OBJECT|CRIT: Can not parse declaration for non-existing class $class"; | ||
| 76 | |||
| 77 | my $node = Crop::Object::Warehouse::Lang::SQL::Query::Select::Node::Bundle->new( | ||
| 78 | class => $class, | ||
| 79 | table => $class->Table, | ||
| 80 | table_effective => $class->Table, | ||
| 81 | attr => $class->Attributes(STORED), | ||
| 82 | # clause => $clause, # should be calculated by calling code | ||
| 83 | parent => $parent, | ||
| 84 | parent_link => $self->{xattr}, | ||
| 85 | parent_view => $self->{view}, | ||
| 86 | ); | ||
| 87 | |||
| 88 | $parent->child->Push($node); | ||
| 89 | |||
| 90 | $node; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; |