Changeset 17248

Show
Ignore:
Timestamp:
08/08/08 15:41:52 (5 years ago)
Author:
daisuke
Message:

Add has_dom_children

Location:
lang/perl/MooseX-DOM/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM.pm

    r17247 r17248  
    6161=head2 has_dom_child $name[, %opts] 
    6262 
    63 Specifies that the object should contain a single child by the given name 
     63Specifies that the object should contain a single child by the given name. 
     64Will generate accessor that can handle set/get 
     65 
     66  has_dom_child 'foo'; 
     67 
     68  $obj->foo(); # get the value of child element foo 
     69  $obj->foo("bar"); # set the value of child element foo to bar 
     70 
     71=head2 has_dom_children  
     72 
     73Specifies that the object should contain possibly multiple children by the 
     74given name 
     75 
     76  has_dom_children 'foo'; 
     77 
     78  $obj->foo(); # Returns a list of values for each child element foo 
     79  $obj->foo(qw(1 2 3)); # Discards old values of foo, and create new nodes 
    6480 
    6581=cut 
  • lang/perl/MooseX-DOM/trunk/lib/MooseX/DOM/LibXML.pm

    r17247 r17248  
    5656 
    5757    my %exports = ( 
     58        has_dom_children => sub { 
     59            return Class::MOP::subname($subname->('has_dom_children') => sub ($;%) { 
     60                my $caller = caller(); 
     61                my($name, %args) = @_; 
     62                my $namespace = $args{namespace} ||= DEFAULT_NAMESPACE_PREFIX; 
     63                my $tagname   = $args{tag} || $name; 
     64                my $filter    = $args{filter} || sub { 
     65                    my $self = shift; 
     66                    return map { $_->textContent } @_; 
     67                }; 
     68 
     69                my $method = $subname->($name, $caller); 
     70                $subassign->($method => sub { 
     71                    my $self = shift; 
     72                    my $node = $self->node; 
     73                    my $nsuri = $self->namespaces->{ $namespace }; 
     74                    my @children = ($nsuri) ? 
     75                        $node->getChildrenByTagNameNS($nsuri, $tagname): 
     76                        $node->getChildrenByTagName($tagname) 
     77                    ; 
     78 
     79                    if (@_) { 
     80                        $node->removeChild($_) for @children; 
     81                        my $document = $node->ownerDocument; 
     82                        foreach my $data (@_) { 
     83                            my $child = ($nsuri) ? 
     84                                $document->createElementNS($nsuri, $tagname) : 
     85                                $document->createElement($tagname) 
     86                            ; 
     87                            $child->appendTextNode($data); 
     88                            push @children, $child; 
     89                            $node->appendChild($child); 
     90                        } 
     91                    } 
     92 
     93                    return $filter->($self, @children); 
     94                }); 
     95            }); 
     96        }, 
    5897        has_dom_child => sub { 
    5998            return Class::MOP::subname($subname->('has_dom_child') => sub ($;%) { 
     
    102141        goto &$export; 
    103142    } 
     143} 
    104144 
     145sub as_xml { 
     146    my $self = shift; 
     147    $self->node->toString(1); 
    105148} 
    106149 
  • lang/perl/MooseX-DOM/trunk/t/01_simple.t

    r17247 r17248  
    11use strict; 
    22use lib "t/lib"; 
    3 use Test::More (tests => 5); 
     3use Test::More (tests => 7); 
    44 
    55BEGIN 
     
    1212<feed xmlns="http://www.w3.org/2005/Atom"> 
    1313<title>Hoge</title> 
     14<multi>multi1</multi> 
     15<multi>multi2</multi> 
     16<multi>multi3</multi> 
     17<multi>multi4</multi> 
    1418</feed> 
    1519EOXML 
     
    1822    ok($obj); 
    1923    isa_ok($obj, 'Test::MxD::Simple'); 
     24 
    2025    is( $obj->title, "Hoge" ); 
    2126 
     
    2328 
    2429    is( $obj->title, "Wee" ); 
     30 
     31    my @multi = $obj->multi; 
     32    is_deeply(\@multi,[ 'multi1', 'multi2', 'multi3', 'multi4' ] ); 
     33 
     34    $obj->multi('multi5', 'multi6'); 
     35    @multi = $obj->multi; 
     36    is_deeply(\@multi,[ 'multi5', 'multi6' ]); 
    2537} 
  • lang/perl/MooseX-DOM/trunk/t/lib/Test/MxD/Simple.pm

    r17247 r17248  
    77has_dom_child 'title'; 
    88 
     9has_dom_children 'multi'; 
     10 
    9111;