| 1 | package FindApp::Object::State; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use mro "c3"; | ||
| 7 | |||
| 8 | use FindApp::Utils qw(:all); | ||
| 9 | use FindApp::Vars qw(:all); | ||
| 10 | BEGIN { | ||
| 11 | subuse "Group"; | ||
| 12 | subuse "Group::State::Dirs"; | ||
| 13 | } | ||
| 14 | use namespace::clean; | ||
| 15 | |||
| 16 | ################################################################ | ||
| 17 | |||
| 18 | sub canonical_subdirs { | ||
| 19 | # There may not be a singleton yet, and if not, we cannot | ||
| 20 | # autogenerate one at this point, since that needs rootdir etc. | ||
| 21 | return +BLM unless defined &rootdir; # FIXME: wrong namespace for &rootdir | ||
| 22 | my $self = &myself; | ||
| 23 | return $self->subgroup_names; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub exported_envars() { | ||
| 27 | state $vars = [ | ||
| 28 | qw($Root), | ||
| 29 | subdir_map { map { ('$'.$_, '@'.$_) } ucfirst }, | ||
| 30 | ]; | ||
| 31 | return @$vars; | ||
| 32 | } | ||
| 33 | |||
| 34 | ################################################################ | ||
| 35 | # Declare object attributes... | ||
| 36 | use pluskeys qw{ | ||
| 37 | ORIGIN | ||
| 38 | DEFAULT | ||
| 39 | GROUPS | ||
| 40 | }; | ||
| 41 | |||
| 42 | # And define their accessors: | ||
| 43 | |||
| 44 | sub allocate_groups { | ||
| 45 | my $self = &myself; | ||
| 46 | $$self{+GROUPS} = { alldir_map { $_ => subpackage("Group")->new($_) } }; | ||
| 47 | } | ||
| 48 | |||
| 49 | # Attribute group: hash mapping directory grouping names to f:o:s:Group objects. | ||
| 50 | sub group { | ||
| 51 | my $self = &myself; | ||
| 52 | bad_args(@_ > 1); | ||
| 53 | if (@_) { | ||
| 54 | return $$self{+GROUPS}{lc shift}; | ||
| 55 | } else { | ||
| 56 | return $$self{+GROUPS}; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | # All the group names. | ||
| 61 | sub group_names { | ||
| 62 | my $self = &myself; | ||
| 63 | bad_args(@_ > 0); | ||
| 64 | return keys %{ $self->group }; | ||
| 65 | } | ||
| 66 | |||
| 67 | # All the individual groups. | ||
| 68 | sub groups { | ||
| 69 | my $self = &myself; | ||
| 70 | bad_args(@_ > 0); | ||
| 71 | return values %{ $self->group }; | ||
| 72 | } | ||
| 73 | |||
| 74 | # Group names excluding root. | ||
| 75 | sub subgroup_names { | ||
| 76 | my $self = &myself; | ||
| 77 | bad_args(@_ > 0); | ||
| 78 | return sort grep { $_ ne "root" } keys %{ $self->group }; | ||
| 79 | } | ||
| 80 | |||
| 81 | # Groups excluding the root group. | ||
| 82 | sub subgroups { | ||
| 83 | my $self = &myself; | ||
| 84 | bad_args(@_ > 0); | ||
| 85 | return @{ $self->group } | ||
| 86 | { $self->subgroup_names }; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Attribute default_origin. | ||
| 90 | sub default_origin { | ||
| 91 | my $self = &myself; | ||
| 92 | bad_args(@_ > 1); | ||
| 93 | if (@_) { | ||
| 94 | my($where) = @_; | ||
| 95 | $where =~ /^(?:cwd|script)$/ | ||
| 96 | || croak "default origin must be either 'cwd' or 'script', not '$where'"; | ||
| 97 | $$self{+DEFAULT} = $where; | ||
| 98 | } | ||
| 99 | return $$self{+DEFAULT} || "script"; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub has_origin { | ||
| 103 | my $self = &myself; | ||
| 104 | bad_args(@_ != 0); | ||
| 105 | return defined $$self{+ORIGIN}; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub prefers_dot { | ||
| 109 | my $self = &myself; | ||
| 110 | bad_args(@_ != 0); | ||
| 111 | $self->default_origin eq "cwd"; | ||
| 112 | } | ||
| 113 | |||
| 114 | # Attribute origin. | ||
| 115 | sub origin { | ||
| 116 | my $self = &myself; | ||
| 117 | bad_args(@_ > 1); | ||
| 118 | |||
| 119 | if (@_) { | ||
| 120 | my $origin = shift; | ||
| 121 | bad_args(!defined $origin); | ||
| 122 | $$self{+ORIGIN} = $origin; | ||
| 123 | } | ||
| 124 | elsif (!$self->has_origin) { | ||
| 125 | $$self{+ORIGIN} = $self->default_origin eq "cwd" | ||
| 126 | ? getcwd() | ||
| 127 | : $FindBin::Bin; | ||
| 128 | } | ||
| 129 | |||
| 130 | return $$self{+ORIGIN}; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub reset_origin { | ||
| 134 | my $self = &myself; | ||
| 135 | bad_args(@_ > 0); | ||
| 136 | my $old_origin = $self->origin; | ||
| 137 | undef $$self{+ORIGIN}; | ||
| 138 | return $old_origin; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | # Pseudo-attribute: app_root | ||
| 143 | |||
| 144 | sub has_app_root { | ||
| 145 | my $self = &myself; | ||
| 146 | good_args(@_ == 0); | ||
| 147 | return !!$self->rootdir->found->count; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub app_root { | ||
| 151 | my $self = &myself; | ||
| 152 | good_args(@_ == 0); | ||
| 153 | return $self->rootdir->found->first; | ||
| 154 | } | ||
| 155 | |||
| 156 | 1; | ||
| 157 | |||
| 158 | =encoding utf8 | ||
| 159 | |||
| 160 | =head1 NAME | ||
| 161 | |||
| 162 | FindApp::Object::State - FIXME | ||
| 163 | |||
| 164 | =head1 SYNOPSIS | ||
| 165 | |||
| 166 | use FindApp::Object::State; | ||
| 167 | |||
| 168 | =head1 DESCRIPTION | ||
| 169 | |||
| 170 | =head2 Pluskey Attributes | ||
| 171 | |||
| 172 | These are always private; use the methods insteads. | ||
| 173 | |||
| 174 | =over | ||
| 175 | |||
| 176 | =item DEFAULT | ||
| 177 | |||
| 178 | Either "cwd" or "script", | ||
| 179 | |||
| 180 | =item GROUPS | ||
| 181 | |||
| 182 | Ref to hash mapping directory grouping names to L<FindApp:Object:State:Group> objects. | ||
| 183 | |||
| 184 | =item ORIGIN | ||
| 185 | |||
| 186 | Directory to start the search from. If unset, either the working directory or C<$FindBin::Bin> | ||
| 187 | depending on the value of L</DEFAULT>. | ||
| 188 | |||
| 189 | =back | ||
| 190 | |||
| 191 | =head2 Methods | ||
| 192 | |||
| 193 | =over | ||
| 194 | |||
| 195 | =item allocate_groups | ||
| 196 | |||
| 197 | =item app_root | ||
| 198 | |||
| 199 | =item canonical_subdirs | ||
| 200 | |||
| 201 | =item default_origin | ||
| 202 | |||
| 203 | =item exported_envars | ||
| 204 | |||
| 205 | =item group | ||
| 206 | |||
| 207 | =item group_names | ||
| 208 | |||
| 209 | =item groups | ||
| 210 | |||
| 211 | =item has_app_root | ||
| 212 | |||
| 213 | =item has_origin | ||
| 214 | |||
| 215 | =item origin | ||
| 216 | |||
| 217 | =item prefers_dot | ||
| 218 | |||
| 219 | =item reset_origin | ||
| 220 | |||
| 221 | =item subgroups | ||
| 222 | |||
| 223 | =item subgroup_names | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =head1 EXAMPLES | ||
| 228 | |||
| 229 | =head1 ENVIRONMENT | ||
| 230 | |||
| 231 | =head1 SEE ALSO | ||
| 232 | |||
| 233 | =over | ||
| 234 | |||
| 235 | =item L<FindApp> | ||
| 236 | |||
| 237 | =back | ||
| 238 | |||
| 239 | =head1 CAVEATS AND PROVISOS | ||
| 240 | |||
| 241 | =head1 BUGS AND LIMITATIONS | ||
| 242 | |||
| 243 | =head1 HISTORY | ||
| 244 | |||
| 245 | =head1 AUTHOR | ||
| 246 | |||
| 247 | Tom Christiansen C<< <tchrist@perl.com> >> | ||
| 248 | |||
| 249 | =head1 LICENCE AND COPYRIGHT | ||
| 250 | |||
| 251 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 252 | All Rights Reserved. | ||
| 253 | |||
| 254 | This module is free software; you can redistribute it and/or | ||
| 255 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 256 | |||
| 257 | =cut | ||
| 258 |