| 1 | # Copyright (c) 2024-2025 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.16; | ||
| 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__ | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =encoding UTF-8 | ||
| 144 | |||
| 145 | =head1 NAME | ||
| 146 | |||
| 147 | File::Information::Editor - generic module for extracting information from filesystems | ||
| 148 | |||
| 149 | =head1 VERSION | ||
| 150 | |||
| 151 | version v0.16 | ||
| 152 | |||
| 153 | =head1 SYNOPSIS | ||
| 154 | |||
| 155 | use File::Information; | ||
| 156 | |||
| 157 | my File::Information::Editor $editor = $obj->editor; | ||
| 158 | |||
| 159 | # ... | ||
| 160 | |||
| 161 | $editor->apply; | ||
| 162 | |||
| 163 | This represents an editor that allows to edit another object. | ||
| 164 | |||
| 165 | =head1 METHODS | ||
| 166 | |||
| 167 | =head2 instance | ||
| 168 | |||
| 169 | my File::Information $instance = $editor->instance; | ||
| 170 | |||
| 171 | Returns the instance that was used to create this object. | ||
| 172 | |||
| 173 | =head2 parent | ||
| 174 | |||
| 175 | my $parent = $editor->parent; | ||
| 176 | |||
| 177 | Returns the parent that was used to create this object. | ||
| 178 | |||
| 179 | =head2 status | ||
| 180 | |||
| 181 | my $status = $editor->status; | ||
| 182 | |||
| 183 | Returns the status of the editor. | ||
| 184 | |||
| 185 | =head2 tagpool | ||
| 186 | |||
| 187 | my @tagpool = $editor->tagpool; | ||
| 188 | |||
| 189 | Returns the list of tagpools found relevant to this editor if any (See L<File::Information::Tagpool>). | ||
| 190 | |||
| 191 | B<Note:> | ||
| 192 | There is no order to the returned values. The order may change between any two calls. | ||
| 193 | |||
| 194 | =head2 apply | ||
| 195 | |||
| 196 | $editor->apply; | ||
| 197 | |||
| 198 | Applies the changes to the file for a root editor. | ||
| 199 | Queues changes for apply on sub-editors to the root editor. | ||
| 200 | |||
| 201 | B<Note:> | ||
| 202 | When using sub-editors one must call this on all sub-editors and | ||
| 203 | then on the root editor to apply the changes. | ||
| 204 | |||
| 205 | See also: | ||
| 206 | L</for>. | ||
| 207 | |||
| 208 | =head2 for | ||
| 209 | |||
| 210 | my File::Information::Editor $sub_editor = $editor->for(key => value, ...); | ||
| 211 | |||
| 212 | Returns an sub-editor with altered options. | ||
| 213 | |||
| 214 | See also: | ||
| 215 | L</apply>. | ||
| 216 | |||
| 217 | =head1 AUTHOR | ||
| 218 | |||
| 219 | Philipp Schafft <lion@cpan.org> | ||
| 220 | |||
| 221 | =head1 COPYRIGHT AND LICENSE | ||
| 222 | |||
| 223 | This software is Copyright (c) 2024-2025 by Philipp Schafft <lion@cpan.org>. | ||
| 224 | |||
| 225 | This is free software, licensed under: | ||
| 226 | |||
| 227 | The Artistic License 2.0 (GPL Compatible) | ||
| 228 | |||
| 229 | =cut |