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 4     4   48 use warnings;
  4         9  
  4         148  
4 4     4   31 use strict;
  4         9  
  4         98  
5 4     4   25 use Types::Standard qw(ArrayRef Str);
  4         74  
  4         89  
6              
7 4     4   3354 use List::Util qw(shuffle);
  4         8  
  4         240  
8 4     4   28 use Try::Tiny;
  4         9  
  4         196  
9              
10 4     4   1705 use Sweat::Drill;
  4         16  
  4         170  
11              
12 4     4   36 use Moo;
  4         9  
  4         22  
13 4     4   10985 use namespace::clean;
  4         12  
  4         41  
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 12     12 0 7410 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 12         30 $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 15     15 0 24 my $self = shift;
47              
48 15         48 my %restriction_map = (
49             jumping => 'requires_jumping',
50             chair => 'requires_a_chair',
51             );
52              
53 15         21 for my $drill ( @{ $self->drills } ) {
  15         48  
54 45         765 $drill->is_used(0);
55 45         1041 for my $restriction ( keys %restriction_map ) {
56 90         225 my $attribute = $restriction_map{$restriction};
57 90 50 66     633 if ( $drill->$attribute && !$self->sweat->$restriction ) {
58 0         0 $drill->is_used(1);
59 0         0 last;
60             }
61             }
62             }
63              
64 15 50       81 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 3     3 0 48 my ( $class, $sweat, $data ) = @_;
73 3         9 my @groups;
74             try {
75 3     3   147 for my $group_data ( @$data ) {
76 12         24 my @drills;
77 12         18 for my $drill_data ( @{$group_data->{drills} } ) {
  12         36  
78 36         633 my $drill = Sweat::Drill->new( $drill_data );
79 36         15339 push @drills, $drill;
80             }
81             my $group = $class->new(
82             drills => \@drills,
83             name => $group_data->{name},
84 12         189 sweat => $sweat,
85             );
86 12         141 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 3         39 };
92 3         153 return @groups;
93             }
94              
95             sub unused_drills {
96 57     57 0 69 my $self = shift;
97              
98 57         84 return grep { not $_->is_used } @{ $self->drills };
  171         3051  
  57         108  
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>