| 1 | package Forest::Tree::Loader::SimpleUIDLoader; | ||||||||||
| 2 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 3 | our $VERSION = '0.10'; | |||||||||
| 5 | 3 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||
| 6 | |||||||||||
| 7 | 3 | with 'Forest::Tree::Loader'; | |||||||||
| 8 | |||||||||||
| 9 | has 'row_parser' => ( | ||||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | isa => 'CodeRef', | ||||||||||
| 12 | 2 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 40 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 40 | my $row = shift; | |||||||||
| 15 | $row->{node}, $row->{uid}, $row->{parent_uid} | ||||||||||
| 16 | 40 | } | |||||||||
| 17 | 2 | }, | |||||||||
| 18 | 3 | ); | |||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||
| 21 | 2 | my ($self, $table) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 2 | my $root = $self->tree; | |||||||||
| 24 | 2 | my $row_parser = $self->row_parser; | |||||||||
| 25 | |||||||||||
| 26 | 2 | my %index; | |||||||||
| 27 | |||||||||||
| 28 | 2 | foreach my $row (@$table) { | |||||||||
| 29 | 20 | my ($node, $uid, undef) = $row_parser->($row); | |||||||||
| 30 | # NOTE: uids MUST be true values ... | ||||||||||
| 31 | 20 | ▶ | if ($uid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 20 | my $t = $self->create_new_subtree( | |||||||||
| 33 | node => $node, | ||||||||||
| 34 | uid => $uid, | ||||||||||
| 35 | ); | ||||||||||
| 36 | 20 | $index{ $uid } = $t; | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 2 | my @orphans; | |||||||||
| 41 | 2 | foreach my $row (@$table) { | |||||||||
| 42 | 20 | my (undef, $uid, $parent_uid) = $row_parser->($row); | |||||||||
| 43 | # NOTE: uids MUST be true values ... | ||||||||||
| 44 | 20 | ▶ | if ($uid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 20 | my $tree = $index{ $uid }; | |||||||||
| 46 | 20 | ▶ | if (my $parent = $index{ $parent_uid }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 12 | $parent->add_child($tree); | |||||||||
| 48 | } | ||||||||||
| 49 | else { | ||||||||||
| 50 | 8 | push @orphans => $tree; | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | ▶ | if (@orphans) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 2 | $root->add_children(@orphans); | |||||||||
| 57 | } | ||||||||||
| 58 | else { | ||||||||||
| 59 | 0 | $root->add_child( $index{ (sort keys %index)[0] } ); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 2 | $root; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 66 | |||||||||||
| 67 | 3 | ▶ | no Moose; 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | |||||||||||
| 69 | __END__ | ||||||||||