root/lang/perl/Text-Nyarlax/trunk/lib/Text/Nyarlax/Element.pm @ 105

Revision 105, 4.0 kB (checked in by nyarla, 6 years ago)

lang/perl/Text-Nyarlax: I moved Text::Nyarlax::Element->join_content to Text::Nyarlax::Util::join_node.

Line 
1package Text::Nyarlax::Element;
2
3use strict;
4use warnings;
5
6our $VERSION = '0.1';
7
8use Carp;
9use base qw( Class::Accessor::Fast );
10__PACKAGE__->mk_ro_accessors( qw( name attribute ) );
11use Text::Nyarlax::Util qw( join_node );
12
13{
14    no warnings 'uninitialized';
15    *attr = __PACKAGE__->can('attribute');
16}
17
18sub new {
19    my ( $class, %args ) = @_;
20
21    my $name = $args{'name'}
22        or croak "Element name is not specified.";
23
24    my $attr = $args{'attribute'} || $args{'attr'} || {};
25    croak "Attribute is not HASH reference." if ( ref $attr ne 'HASH' );
26
27    my $self = bless {
28        name        => $name,
29        attribute   => $attr,
30        content     => [],
31    }, $class;
32
33    my $content = $args{'content'} || [];
34       $content = [ $content ] if ( ref $content ne 'ARRAY' );
35
36    $self->content( $content );
37
38    return $self;
39}
40
41sub parent {
42    my $self = shift;
43    if ( @_ ) {
44        my $parent = shift @_;
45        croak "Argument is not " . __PACKAGE__ . " object."
46            if ( ref $parent ne __PACKAGE__ );
47        $self->{'parent'} = $parent;
48    }
49    else {
50        $self->{'parent'};
51    }
52}
53
54sub content {
55    my $self = shift;
56    if ( @_ ) {
57        my $contents = shift @_;
58           $contents = [ $contents ] if ( ref $contents ne 'ARRAY' );
59        for my $content ( @{ $contents } ) {
60            $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
61        }
62        $self->{'content'} = $contents;
63    }
64    else {
65        $self->{'content'};
66    }
67}
68
69sub shift_content {
70    my ( $self ) = @_;
71    shift @{ $self->{'content'} };
72}
73
74sub unshift_content {
75    my ( $self, @contents ) = @_;
76    for my $content ( @contents ) {
77        $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
78    }
79    unshift @{ $self->{'content'} }, @contents;
80}
81
82sub pop_content {
83    my ( $self ) = @_;
84    pop @{ $self->{'content'} };
85}
86
87sub push_content {
88    my ( $self, @contents ) = @_;
89    for my $content ( @contents ) {
90        $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
91    }
92    push @{ $self->{'content'} }, @contents;
93}
94
95sub join_content {
96    my ( $self, %args ) = @_;
97    $self->content( join_node( $self->content, %args ) );
98}
99
1001;
101__END__
102
103=head1 NAME
104
105Text::Nyarlax::Element - Text::Nyarlax element object
106
107=head1 SYNOPSIS
108
109    use Text::Nyarlax::Element;
110   
111    my $elm = Text::Nyarlax::Element->new(
112        name => 'Section',
113        attr => {
114            id => 'MyID',
115            class => 'MyClass',
116        },
117        content => [
118            'Foo',
119            'Bar',
120            'Baz'
121        ],
122    );
123
124=head1 DESCRIPTION
125
126Text::Nyarlax::Element is the object to compose the element tree.
127The element tree is generated by the parser of Text::Nyarlax.
128
129=head1 METHODS
130
131=head2 new
132
133The argument is three of name, attribute(attr) and content, and name is necessary.
134
135attribute has to be Hash and content has to be one character string,Text::Nyarlax::Element object
136or ARRAY which consists of a character string and Text::Nyarlax::element object.
137
138=head2 name
139
140get element name. readonly.
141
142=head2 attribute/attr
143
144get element attributes. return HASH reference.
145
146=head2 parent
147
148set/get the Text::Nyarlax::Element object which becomes a parent.
149
150=head2 content
151
152set/get the contents of Text::Nyarlax::Element object.
153
154When setting the contents, it's necessary to use ARRAY reference,
155and when getting the contents, ARRAY reference is returning.
156
157=head2 shift_content
158
159The contents are added at the first of obejct contents.
160
161=head2 unshift_content
162
163The first contents is taken out.
164
165=head2 push_content
166
167The contents are added at the last of obejct contents.
168
169=head2 pop_content
170
171The last contents is taken out.
172
173Naoki Okamura E<lt>thotep@nayrla.netE<gt>
174
175=head1 LICENSE
176
177This library is free software; you can redistribute it and/or modify
178it under the same terms as Perl itself.
179
180=head1 SEE ALSO
181
182L<Text::Nyarlax::Element::Content>
183
184=cut;
Note: See TracBrowser for help on using the browser.