Atomik/Element.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« Atomik/Content.pm ↑ Summary Atomik/Entry.pm »
1 # $Id: /mirror/coderepos/lang/perl/Atomik/trunk/lib/Atomik/Element.pm 68160 2008-08-10T23:55:31.147997Z daisuke $
2  
3 package Atomik::Element;
4 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
5  
6 has 'storage' => ( is => 'rw', does => 'Atomik::Storage' );
7  
8 has 'namespace' => (
9 is => 'rw',
10 isa => 'Str',
11 );
12  
13 has 'version' => (
14 is => 'rw',
15 isa => 'Num',
16 );
17  
18 no Moose;
19  
20 my %NS2VERSION = (
21 "http://purl.org/atom/ns#" => '0.3',
22 "http://www.w3.org/2005/Atom" => '1.0',
23 "http://www.w3.org/2007/app" => '1.0', # AtomPub
24 );
25  
26 sub BUILD {
27 my $self = shift;
28  
29 if (my $node = $self->node) {
30 my $namespace = $node->namespaceURI();
31 $self->namespace( $namespace );
32 $self->version( $NS2VERSION{ $namespace } );
33 }
34 return $self;
35 }
36  
37 sub from_any {
38 my ($class, $any) = @_;
39  
40 confess "no argument given to from_any" unless $any;
41  
42 my $blessed = Scalar::Util::blessed $any;
43 if ($blessed) {
44 if ($any->can('toString')) {
45 return $class->from_xml($any->toString());
46 } elsif ($any->can('as_xml')) {
47 return $class->from_xml($any->as_xml());
48 }
49  
50 confess "don't know how to handle $any";
51 }
52  
53 my $reftype = Scalar::Util::reftype $any || '';
54 if (! $reftype) {
55 confess "XXX - Later (from file)";
56 }
57  
58 if ($reftype eq 'SCALAR') {
59 return $class->from_xml($$any);
60 }
61  
62 confess "don't know how to handle $any";
63 }
64  
65 sub element_get {
66 my ($self, %args) = @_;
67  
68 my @nodes = $self->storage->findnodes_from_tagname(
69 tag => $args{tag},
70 namespace => $args{namespace},
71 strip => 1,
72 );
73  
74 return wantarray ? @nodes : $nodes[0];
75 }
76  
77  
78 sub __mk_element_accessor {
79 my $class = shift;
80 my $element = shift;
81 my $code = sprintf(<<'EOSUB', blessed $class || $class, $element, $element);
82 sub %s::%s {
83 my $self = shift;
84 my $namespace = $self->namespace;
85 my $tag = '%s';
86 return @_ ?
87 $self->element_set(namespace => $namespace, tag => $tag, value => $_[0]) :
88 $self->element_get(namespace => $namespace, tag => $tag);
89 }
90 EOSUB
91 eval $code;
92 confess $@ if $@;
93 }
94  
95  
96 # These accessors must differ how they act depending on if the feed is
97 1;
« Atomik/Content.pm ↑ Summary Atomik/Entry.pm »