« lib/BioX/Workflow/Command/inspect/Utils/ParsePlainText.pm
↑ Summary
lib/BioX/Workflow/Command/stats.pm »
| 1 | package BioX::Workflow::Command::run::Rules::Directives::Walk; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | use Memoize; | ||
| 7 | use Data::Walk 2.01; | ||
| 8 | use Path::Tiny; | ||
| 9 | |||
| 10 | has 'errors' => ( | ||
| 11 | is => 'rw', | ||
| 12 | isa => 'HashRef', | ||
| 13 | default => sub {return {}}, | ||
| 14 | ); | ||
| 15 | |||
| 16 | sub walk_process_data { | ||
| 17 | my $self = shift; | ||
| 18 | my $keys = shift; | ||
| 19 | |||
| 20 | foreach my $k ( @{$keys} ) { | ||
| 21 | next if ref($k); | ||
| 22 | my $v = $self->$k; | ||
| 23 | ##Leftover of backwards compatibility | ||
| 24 | if ( $k eq 'find_by_dir' ) { | ||
| 25 | $self->process_directive( $k, $v ); | ||
| 26 | } | ||
| 27 | ##If its a type search for the type | ||
| 28 | elsif ( $self->search_registered_process_directives( $k, $v ) ) { | ||
| 29 | next; | ||
| 30 | } | ||
| 31 | else { | ||
| 32 | $self->process_directive( $k, $v ); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | ##TODO Combine this with search_registered_types | ||
| 38 | sub search_registered_process_directives { | ||
| 39 | my $self = shift; | ||
| 40 | my $k = shift; | ||
| 41 | my $v = shift; | ||
| 42 | |||
| 43 | foreach my $key ( keys %{ $self->register_process_directives } ) { | ||
| 44 | $DB::single = 2; | ||
| 45 | next unless exists $self->register_process_directives->{$key}->{lookup}; | ||
| 46 | next | ||
| 47 | unless exists $self->register_process_directives->{$key}->{builder}; | ||
| 48 | my $lookup_ref = $self->register_process_directives->{$key}->{lookup}; | ||
| 49 | my $builder = $self->register_process_directives->{$key}->{builder}; | ||
| 50 | |||
| 51 | foreach my $lookup ( @{$lookup_ref} ) { | ||
| 52 | if ( $k =~ m/$lookup/ ) { | ||
| 53 | $self->$builder( $k, $v ); | ||
| 54 | return 1; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head3 process_directive | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub process_directive { | ||
| 67 | my $self = shift; | ||
| 68 | my $k = shift; | ||
| 69 | my $v = shift; | ||
| 70 | my $path = shift; | ||
| 71 | |||
| 72 | #TODO Need to keep track of errors here | ||
| 73 | if ( ref($v) ) { | ||
| 74 | walk { | ||
| 75 | wanted => sub { $self->walk_directives(@_) } | ||
| 76 | }, | ||
| 77 | $self->$k; | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | my $text = ''; | ||
| 81 | $text = $self->interpol_directive($v) if $v; | ||
| 82 | $self->$k($text); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | =head3 walk_directives | ||
| 87 | |||
| 88 | Invoke with | ||
| 89 | walk { wanted => sub { $self->directives(@_) } }, $self->other_thing; | ||
| 90 | |||
| 91 | Acts funny with $self->some_other_thing is not a reference | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub walk_directives { | ||
| 96 | my $self = shift; | ||
| 97 | my $ref = shift; | ||
| 98 | |||
| 99 | return if ref($ref); | ||
| 100 | return unless $ref; | ||
| 101 | |||
| 102 | my $text = ''; | ||
| 103 | $text = $self->interpol_directive($ref) if $ref; | ||
| 104 | $self->update_directive($text); | ||
| 105 | } | ||
| 106 | |||
| 107 | =head3 update_directive | ||
| 108 | |||
| 109 | Take the values from walk_directive and update the directive | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub update_directive { | ||
| 114 | my $self = shift; | ||
| 115 | my $text = shift; | ||
| 116 | |||
| 117 | my ( $key, $container, $index ); | ||
| 118 | |||
| 119 | $container = $Data::Walk::container; | ||
| 120 | $key = $Data::Walk::key; | ||
| 121 | $index = $Data::Walk::index; | ||
| 122 | |||
| 123 | if ( $Data::Walk::type eq 'HASH' && $key ) { | ||
| 124 | $container->{$key} = $text; | ||
| 125 | } | ||
| 126 | elsif ( $Data::Walk::type eq 'ARRAY' ) { | ||
| 127 | $container->[$index] = $text; | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | #We are getting the whole hash, just return | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; |