File Coverage

blib/lib/Weewar/Base.pm
Criterion Covered Total %
statement 15 70 21.4
branch 0 16 0.0
condition 0 6 0.0
subroutine 5 14 35.7
pod n/a
total 20 106 18.8


line stmt bran cond sub pod time code
1             # Copyright (c) 2007 Jonathan Rockway
2              
3             package Weewar::Base;
4 1     1   6 use strict;
  1         2  
  1         29  
5 1     1   5 use warnings;
  1         2  
  1         24  
6 1     1   5 use Carp;
  1         1  
  1         48  
7 1     1   1132 use DateTime::Format::RSS;
  1         416506  
  1         13  
8              
9             require Weewar;
10             require Weewar::User;
11             require Weewar::Game;
12              
13 1     1   172 use base 'Class::Accessor';
  1         2  
  1         957  
14              
15             sub mk_weewar_accessors {
16 0     0     my $class = shift;
17 0           $class->mk_ro_accessors
18             ( map {
19 0           my $a = $_;
20 0           $a =~ s/([a-z])([A-Z])/$1.'_'.(lc $2)/eg;
  0            
21 0           $a;
22             }
23 0           ($class->_ATTRIBUTES, $class->_ELEMENTS, keys %{{$class->_LISTS()}}));
24             }
25              
26             sub get {
27 0     0     my ($self, $what) = @_;
28 0           $what =~ s/_([a-z])/uc $1/ge; # perl_style to javaStyle
  0            
29            
30 0 0         if(exists $self->{$what}){
31 0           my $retval = $self->{$what};
32 0 0 0       return @$retval if(ref $retval && ref $retval eq 'ARRAY');
33 0           return $retval;
34             }
35            
36             # data hasn't been loaded yet, so load it
37 0           my $xml = $self->_get_xml;
38 0           my $root_tag = [$xml->getElementsByTagName($self->_root_tag)]->[0];
39              
40              
41              
42             # get stuff that's in the root tag ()
43             $self->_set_element($_ => $root_tag->getAttributeNode($_)->value)
44 0           for ($self->_ATTRIBUTES);
45            
46             # get stuff that's text in a unique element (1502)
47 0           for ($self->_ELEMENTS){
48 0           eval {
49 0           $self->_set_element($_ =>
50             [$root_tag->getElementsByTagName($_)]
51             ->[0]->textContent,
52              
53             );
54             };
55 0 0         if($_ ne 'playingSince'){ # (special cases)++
56 0 0         carp "Expected a $_ tag: $@" if $@;
57             }
58             }
59            
60             # get stuff that's a list (...)
61 0           my %LISTS = $self->_LISTS;
62 0           for my $key (keys %LISTS){
63 0           my ($name, $class, $attribute, $initname, $fixup) = @{$LISTS{$key}};
  0            
64             # name is the name of the element we're inspecting (preferredPlayers)
65             # class is the class of the sub-elements (Weewar::User)
66             # attribute is what we pass to class's constructor (undef = nodetext)
67             # initname is the key that we pass to the constructor
68             # fixup is a sub that's passed the new object and the XML element
69 0   0       $initname ||= $attribute; # defaults to the attribute name
70            
71             my $handler = $attribute ? # if attribute is defined
72 0     0     sub { $_[0]->getAttributeNode($attribute)->value }:# get the attribute
73 0 0   0     sub { $_[0]->textContent }; # otherwise get the text content
  0            
74              
75 0           my @children = [$root_tag->getElementsByTagName($key)]->[0]
76             ->getElementsByTagName($name);
77              
78 0           my @objects = map {$class->new({$initname, $handler->($_)})} @children;
  0            
79              
80 0 0         if($fixup){
81 0           @objects = map { my $xml = shift @children; $fixup->($_, $xml) }
  0            
  0            
82             @objects;
83             }
84 0           $self->_set_element($key => \@objects);
85             }
86            
87 0           return $self->{$what};
88             }
89              
90             sub _set_element {
91 0     0     my $self = shift;
92 0           my $what = shift;
93 0           my $value = shift;
94              
95 0           $self->{$what} = $value;
96            
97 0           my %TRANSFORM = $self->_TRANSFORMS;
98 0           my $transform = $TRANSFORM{$what};
99 0 0         if($transform){
100 0           $self->{$what} = $transform->($self->{$what});
101             }
102              
103 0           return $self->{$what};
104             }
105              
106             sub _TRANSFORM_BOOLEAN {
107 0     0     my $self = shift;
108 0 0   0     return sub { return undef if($_[0] eq 'false'); return $_[0] };
  0            
  0            
109             }
110              
111             sub _TRANSFORM_DATE {
112 0     0     my $self = shift;
113 0     0     return sub { DateTime::Format::RSS->parse_datetime($_[0]) };
  0            
114             }
115              
116             1;
117              
118             __END__