| 1 | # ABSTRACT: Scrappy Scraper Session Handling | ||||||||||||||||||||
| 2 | # Dist::Zilla: +PodWeaver | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package Scrappy::Session; | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 10 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 10 | $Scrappy::Session::VERSION = '0.94112090'; | |||||||||||||||||||
| 8 | } | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | # load OO System | ||||||||||||||||||||
| 11 | 10 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | # load other libraries | ||||||||||||||||||||
| 14 | 10 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 10 | ▶ | use YAML::Syck; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 10 | $YAML::Syck::ImplicitTyping = 1; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 10 | has 'auto_save' => (is => 'rw', isa => 'Bool', default => 1); | |||||||||||||||||||
| 19 | 10 | has 'file' => (is => 'rw', isa => 'Str'); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 0 | ▶ | sub load { | ||||||||||||||||||
|
Subroutine
load: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||||||||||
| 24 | 0 | my $file = shift; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | ▶ | if ($file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | $self->file($file); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | ▶ | croak("Session file $file does not exist or is not read/writable") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | unless -f $file; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # load session file | ||||||||||||||||||||
| 34 | 0 | $self->{stash} = LoadFile($file); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | return $self->{stash}; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | sub stash { | ||||||||||||||||||
|
Subroutine
stash: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||||||||
| 43 | 0 | ▶ | $self->{stash} = {} unless defined $self->{stash}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | ▶ | my $stash = @_ > 1 ? {@_} : $_[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 0 | ▶ | if ($stash) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | 0 | ▶ | if (ref $stash eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | for (keys %{$stash}) { | |||||||||||||||||||
| 50 | 0 | ▶ | if (lc $_ ne ':file') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | $self->{stash}->{$_} = $stash->{$_}; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||
| 54 | 0 | $self->{file} = $stash->{$_}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||
| 59 | 0 | return $self->{stash}->{$stash}; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | $self->auto_write; | |||||||||||||||||||
| 65 | 0 | return $self->{stash}; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub write { | ||||||||||||||||||
|
Subroutine
write: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||
| 71 | 0 | ▶ | my $file = shift || $self->file; | ||||||||||||||||||
|
Condition0%0/3
shift() || $self->file
MC/DC0%0/2
shift() || $self->file
shift()
$self->file
Truth table0%0/3
A || B
A= shift()B= $self->file
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | $self->file($file); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | if ($file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # write session file | ||||||||||||||||||||
| 78 | 0 | DumpFile($file, $self->{stash}); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # ... ummm | ||||||||||||||||||||
| 81 | 0 | ▶ | croak("Session file $file does not exist or is not read/writable") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | unless -f $file; | ||||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | return $self->{stash}; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub auto_write { | ||||||||||||||||||
|
Subroutine
auto_write: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 89 | 0 | my $self = shift; | |||||||||||||||||||
| 90 | 0 | ▶ | $self->write if $self->auto_save; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | return $self; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 10 | 1; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||