File Coverage

blib/lib/Ordeal/Model/Backend/Raw.pm
Criterion Covered Total %
statement 65 75 86.6
branch 13 26 50.0
condition n/a
subroutine 12 14 85.7
pod 4 4 100.0
total 94 119 78.9


line stmt bran cond sub pod time code
1             package Ordeal::Model::Backend::Raw;
2              
3             # vim: ts=3 sts=3 sw=3 et ai :
4              
5 2     2   5049 use 5.020;
  2         8  
6 2     2   11 use strict; # redundant, but still useful to document
  2         4  
  2         53  
7 2     2   10 use warnings;
  2         4  
  2         106  
8             { our $VERSION = '0.004'; }
9 2     2   13 use Mo qw< build default >;
  2         5  
  2         12  
10 2     2   614 use Ouch;
  2         5  
  2         19  
11 2     2   1315 use autodie;
  2         31826  
  2         10  
12              
13 2     2   15321 use experimental qw< postderef signatures >;
  2         4  
  2         16  
14 2     2   384 no warnings qw< experimental::postderef experimental::signatures>;
  2         13  
  2         92  
15              
16 2     2   517 use Ordeal::Model::Card;
  2         4  
  2         57  
17 2     2   12 use Ordeal::Model::Deck;
  2         3  
  2         1502  
18              
19             has data => (default => undef);
20             has _card_for => (default => undef);
21             has _deck_for => (default => undef);
22             has _deck_ids => (default => undef);
23              
24 1     1 1 95 sub BUILD ($self) {
  1         3  
  1         2  
25 1         5 my $data = $self->data;
26 1         20 $self->data(undef); # no need to keep this around any more
27              
28 1 50       11 ouch 400, 'invalid data for Raw backend', $data
29             unless ref($data) eq 'HASH';
30             ouch 400, "invalid 'cards' in data for Raw backend", $data->{cards}
31 1 50       5 unless ref($data->{cards}) eq 'ARRAY';
32             ouch 400, "invalid 'decks' in data for Raw backend", $data->{decks}
33 1 50       8 unless ref($data->{decks}) eq 'ARRAY';
34              
35             # expand and arrange cards in hash, keyed by id
36 1         6 $self->_card_for(\my %card_for);
37 1         10 for my $card ($data->{cards}->@*) {
38 6 50       76 ouch 400, 'invalid card', $card unless ref($card) eq 'HASH';
39 6         10 my $id = $card->{id};
40 6 50       15 ouch 400, 'invalid id in card', $card unless defined $id;
41 6 50       17 ouch 400, 'duplicated card id', $id if exists $card_for{$id};
42 6         31 $card_for{$id} = Ordeal::Model::Card->new($card->%*);
43             } ## end for my $card ($data->{cards...})
44              
45             # expand and arrange decks in hash, keyed by id. Save list of
46             # deck identifiers in the order they appear, too.
47 1         18 $self->_deck_for(\my %deck_for);
48 1         13 $self->_deck_ids(\my @deck_ids);
49 1         11 for my $deck ($data->{decks}->@*) {
50 1 50       6 ouch 400, 'invalid deck', $deck unless ref($deck) eq 'HASH';
51 1         3 my $id = $deck->{id};
52 1 50       5 ouch 400, 'invalid id in deck', $deck unless defined $id;
53 1 50       3 ouch 400, 'duplicated deck id', $id if exists $deck_for{$id};
54              
55             # Resolve cards in deck
56 1         5 my $cards = $deck->{cards};
57 1 50       5 ouch 400, 'invalid cards in deck', $deck
58             unless ref($cards) eq 'ARRAY';
59             my @cs = map {
60 1 50       4 my $card = $card_for{$_} or ouch 404, 'card not found', $_;
  6         64  
61 6         14 $card;
62             } $cards->@*;
63              
64 1         12 $deck_for{$id} = Ordeal::Model::Deck->new(
65             name => $id, # default
66             $deck->%*,
67             cards => \@cs, # override
68             );
69 1         17 push @deck_ids, $deck->{id};
70             } ## end for my $deck ($data->{decks...})
71              
72 1         4 return;
73             } ## end sub BUILD ($self)
74              
75 0     0 1 0 sub card ($self, $id) {
  0         0  
  0         0  
  0         0  
76 0 0       0 my $card = $self->_card_for->{$id} or ouch 404, 'card not found', $id;
77 0         0 return $card;
78             }
79              
80 2     2 1 17 sub deck ($self, $id) {
  2         4  
  2         4  
  2         12  
81 2 100       9 my $deck = $self->_deck_for->{$id} or ouch 404, 'deck not found', $id;
82 1         12 return $deck;
83             }
84              
85 0     0 1   sub decks ($s) { return $s->_deck_ids->@* }
  0            
  0            
  0            
86              
87             1;
88             __END__