| 1 | # Copyright (c) 2024-2026 Philipp Schafft <lion@cpan.org> | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: generic module for extracting information from filesystems | ||
| 6 | |||
| 7 | |||
| 8 | package File::Information::Editor; | ||
| 9 | |||
| 10 | use v5.10; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | |||
| 16 | our $VERSION = v0.17; | ||
| 17 | |||
| 18 | use constant { | ||
| 19 | STATUS_PENDING => 'pending', | ||
| 20 | STATUS_ALL_DONE => 'all_done', | ||
| 21 | STATUS_SOME_DONE => 'some_done', | ||
| 22 | STATUS_NONE_DONE => 'none_done', | ||
| 23 | STATUS_ERROR => 'error', | ||
| 24 | }; | ||
| 25 | |||
| 26 | my %_default_options = ( | ||
| 27 | lifecycle => undef, | ||
| 28 | ); | ||
| 29 | |||
| 30 | |||
| 31 | sub instance { | ||
| 32 | my ($self) = @_; | ||
| 33 | return $self->{instance}; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub parent { | ||
| 38 | my ($self) = @_; | ||
| 39 | return $self->{parent}; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub status { | ||
| 44 | my ($self) = @_; | ||
| 45 | return $self->_self_or_super->{status}; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub tagpool { | ||
| 50 | my ($self) = @_; | ||
| 51 | # We use hash here so we can de-duplicate later on. | ||
| 52 | my %tagpools = map {$_ => $_} eval {$self->inode->tagpool}; | ||
| 53 | return values %tagpools; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub apply { | ||
| 58 | my ($self) = @_; | ||
| 59 | |||
| 60 | if ($self->status eq STATUS_PENDING) { | ||
| 61 | if (defined $self->{super}) { | ||
| 62 | $self->{super}->_queue_request(@{$self->{queue}}); | ||
| 63 | @{$self->{queue}} = (); | ||
| 64 | } else { | ||
| 65 | croak 'Not supported'; | ||
| 66 | } | ||
| 67 | } else { | ||
| 68 | croak 'Cannot apply as the editor is not pending, current state: '.$self->status; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | #@returns __PACKAGE__ | ||
| 74 | sub for { | ||
| 75 | my ($self, %opts) = @_; | ||
| 76 | my $n = __PACKAGE__->_new(parent => $self->parent, super => $self); | ||
| 77 | |||
| 78 | foreach my $key (keys %opts) { | ||
| 79 | croak 'Bad key: '.$key unless exists $_default_options{$key}; | ||
| 80 | } | ||
| 81 | |||
| 82 | $n->{options} = {%{$self->{options}}, %opts}; | ||
| 83 | |||
| 84 | return $n; | ||
| 85 | } | ||
| 86 | |||
| 87 | # ---------------- | ||
| 88 | |||
| 89 | sub _new { | ||
| 90 | my ($pkg, %opts) = @_; | ||
| 91 | my $self = bless \%opts, $pkg; | ||
| 92 | |||
| 93 | $self->{instance} = $self->{parent}->instance unless defined $self->{instance}; | ||
| 94 | |||
| 95 | croak 'No instance is given' unless defined $self->{instance}; | ||
| 96 | croak 'No parent is given' unless defined $self->{parent}; | ||
| 97 | |||
| 98 | $self->{status} = STATUS_PENDING; | ||
| 99 | $self->{options} = {%_default_options}; | ||
| 100 | $self->{queue} = []; | ||
| 101 | |||
| 102 | return $self; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _self_or_super { | ||
| 106 | my ($self) = @_; | ||
| 107 | return defined($self->{super}) ? $self->{super} : $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _inode { | ||
| 111 | my ($self) = @_; | ||
| 112 | |||
| 113 | unless (exists $self->{inode}) { | ||
| 114 | my $parent = $self->_self_or_super->parent; | ||
| 115 | if ($parent->can('inode')) { | ||
| 116 | return $self->{inode} = $parent->inode; | ||
| 117 | } elsif ($parent->isa('File::Information::Inode') || $parent->isa('File::Information::Remote')) { | ||
| 118 | return $self->{inode} = $parent; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | return $self->{inode}; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _queue_request { | ||
| 126 | my ($self, @requests) = @_; | ||
| 127 | my $options = $self->{options}; | ||
| 128 | my @options_keys = keys %{$options}; | ||
| 129 | |||
| 130 | foreach my $request (@requests) { | ||
| 131 | $request->{options} //= {}; | ||
| 132 | $request->{options}->{$_} //= $options->{$_} foreach @options_keys; | ||
| 133 | push(@{$self->{queue}}, $request); | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | __END__ |