File Coverage

blib/lib/Sweat/Group.pm
Criterion Covered Total %
statement 51 55 92.7
branch 2 4 50.0
condition 2 3 66.6
subroutine 13 14 92.8
pod 0 4 0.0
total 68 80 85.0


line stmt bran cond sub pod time code
1             package Sweat::Group;
2              
3 3     3   32 use warnings;
  3         6  
  3         119  
4 3     3   18 use strict;
  3         6  
  3         100  
5 3     3   17 use Types::Standard qw(ArrayRef Str);
  3         6  
  3         70  
6              
7 3     3   2741 use List::Util qw(shuffle);
  3         6  
  3         183  
8 3     3   17 use Try::Tiny;
  3         7  
  3         142  
9              
10 3     3   1314 use Sweat::Drill;
  3         11  
  3         119  
11              
12 3     3   19 use Moo;
  3         6  
  3         13  
13 3     3   7645 use namespace::clean;
  3         8  
  3         26  
14              
15             has 'drills' => (
16             is => 'ro',
17             required => 1,
18             isa => ArrayRef,
19             );
20              
21             has 'name' => (
22             is => 'ro',
23             required => 1,
24             isa => Str,
25             );
26              
27             has 'sweat' => (
28             is => 'ro',
29             required => 1,
30             weaken => 1,
31             );
32              
33             sub BUILD {
34 8     8 0 4918 my ( $self, $args ) = @_;
35              
36             # We reset the drills immediately upon building so that restrictive
37             # preferences (e.g. no-jumping) can get applied immediately.
38 8         20 $self->reset_drills;
39              
40             }
41              
42             # reset_drills: Set the "is_used" bit for each drill to 0, *unless* that drill
43             # is restricted according to current config. In that case, it
44             # gets "is_used" set to 1.
45             sub reset_drills {
46 10     10 0 20 my $self = shift;
47              
48 10         32 my %restriction_map = (
49             jumping => 'requires_jumping',
50             chair => 'requires_a_chair',
51             );
52              
53 10         20 for my $drill ( @{ $self->drills } ) {
  10         26  
54 30         582 $drill->is_used(0);
55 30         676 for my $restriction ( keys %restriction_map ) {
56 60         132 my $attribute = $restriction_map{$restriction};
57 60 50 66     450 if ( $drill->$attribute && !$self->sweat->$restriction ) {
58 0         0 $drill->is_used(1);
59 0         0 last;
60             }
61             }
62             }
63              
64 10 50       38 unless ($self->unused_drills) {
65 0         0 die "The current configuration makes every drill in the "
66             . $self->name
67             . " group unavailable, and we can't have that.\n";
68             }
69             }
70              
71             sub new_from_config_data {
72 2     2 0 38 my ( $class, $sweat, $data ) = @_;
73 2         4 my @groups;
74             try {
75 2     2   106 for my $group_data ( @$data ) {
76 8         12 my @drills;
77 8         16 for my $drill_data ( @{$group_data->{drills} } ) {
  8         20  
78 24         412 my $drill = Sweat::Drill->new( $drill_data );
79 24         9756 push @drills, $drill;
80             }
81             my $group = $class->new(
82             drills => \@drills,
83             name => $group_data->{name},
84 8         126 sweat => $sweat,
85             );
86 8         96 push @groups, $group;
87             }
88             }
89             catch {
90 0     0   0 die "Sorry... I can't parse the provided drill-group config data. $_\n";
91 2         26 };
92 2         78 return @groups;
93             }
94              
95             sub unused_drills {
96 38     38 0 64 my $self = shift;
97              
98 38         48 return grep { not $_->is_used } @{ $self->drills };
  114         1972  
  38         78  
99             }
100              
101             1;
102              
103             =head1 Sweat::Group - Library for the `sweat` command-line program
104              
105             =head1 DESCRIPTION
106              
107             This library is intended for internal use by the L<sweat> command-line program,
108             and as such offers no publicly documented methods.
109              
110             =head1 SEE ALSO
111              
112             L<sweat>
113              
114             =head1 AUTHOR
115              
116             Jason McIntosh <jmac@jmac.org>