| 1 | package Yakuake::Sessions::Base; | ||
| 2 | |||
| 3 | use namespace::autoclean; | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | use Class::Usul::Constants qw( EXCEPTION_CLASS NUL ); | ||
| 7 | use Class::Usul::Functions qw( app_prefix io throw trim ); | ||
| 8 | use Class::Usul::Options; | ||
| 9 | use File::DataClass::Functions qw( map_extension2class supported_extensions ); | ||
| 10 | use File::DataClass::Types qw( ArrayRef Directory HashRef | ||
| 11 | NonEmptySimpleStr Path ); | ||
| 12 | use Scalar::Util qw( blessed ); | ||
| 13 | use Unexpected::Functions qw( Unspecified ); | ||
| 14 | |||
| 15 | extends q(Class::Usul::Programs); | ||
| 16 | |||
| 17 | # Override defaults in base class | ||
| 18 | has '+config_class' => default => sub { 'Yakuake::Sessions::Config' }; | ||
| 19 | |||
| 20 | # Public attributes | ||
| 21 | option 'config_dir' => is => 'lazy', isa => Directory, | ||
| 22 | documentation => 'Directory to store configuration files', | ||
| 23 | coerce => Directory->coercion, format => 's'; | ||
| 24 | |||
| 25 | option 'profile_dir' => is => 'lazy', isa => Path, coerce => Path->coercion, | ||
| 26 | documentation => 'Directory to store the session profiles', | ||
| 27 | default => sub { [ $_[ 0 ]->config_dir, 'profiles' ] }, | ||
| 28 | format => 's'; | ||
| 29 | |||
| 30 | option 'storage_class' => is => 'ro', isa => NonEmptySimpleStr, | ||
| 31 | documentation => 'File format used to store session data', | ||
| 32 | default => sub { $_[ 0 ]->config->storage_class }, format => 's', | ||
| 33 | short => 's'; | ||
| 34 | |||
| 35 | has 'extensions' => is => 'lazy', isa => ArrayRef, | ||
| 36 | builder => sub { [ supported_extensions() ] }, | ||
| 37 | init_arg => undef; | ||
| 38 | |||
| 39 | has 'profile_path' => is => 'lazy', isa => Path, coerce => Path->coercion, | ||
| 40 | init_arg => undef; | ||
| 41 | |||
| 42 | has 'storage_map' => is => 'lazy', isa => HashRef, init_arg => undef; | ||
| 43 | |||
| 44 | # Private methods | ||
| 45 | sub _build_config_dir { | ||
| 46 | my $self = shift; | ||
| 47 | my $home = $self->config->my_home; | ||
| 48 | my $dir = io [ $home, '.'.(app_prefix blessed $self) ]; | ||
| 49 | |||
| 50 | $dir->exists or $dir->mkpath; | ||
| 51 | return $dir; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _build_profile_path { | ||
| 55 | my $self = shift; | ||
| 56 | my $profile = $self->next_argv | ||
| 57 | or throw class => Unspecified, args => [ 'profile name' ]; | ||
| 58 | my $path = io $profile; $path->exists and return $path; | ||
| 59 | my $profdir = $self->profile_dir; $path = $profdir->catfile( $profile ); | ||
| 60 | |||
| 61 | $path->exists and return $path; | ||
| 62 | $profdir->exists or $profdir->mkpath; | ||
| 63 | $profdir->filter( sub { $_->filename =~ m{ \A $profile }mx } ); | ||
| 64 | $path = ($profdir->all_files)[ 0 ]; defined $path and return $path; | ||
| 65 | |||
| 66 | my $extn = $self->storage_map->{ $self->storage_class } || NUL; | ||
| 67 | |||
| 68 | return $profdir->catfile( $profile.$extn ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _build_storage_map { | ||
| 72 | my $self = shift; my $map = {}; | ||
| 73 | |||
| 74 | for my $extn (supported_extensions()) { | ||
| 75 | $map->{ map_extension2class( $extn )->[ 0 ] } = $extn; | ||
| 76 | } | ||
| 77 | |||
| 78 | return $map; | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | __END__ |