File Coverage

blib/lib/Rose/HTML/Object/Message.pm
Criterion Covered Total %
statement 61 75 81.3
branch 15 22 68.1
condition 5 8 62.5
subroutine 16 20 80.0
pod 2 7 28.5
total 99 132 75.0


line stmt bran cond sub pod time code
1             package Rose::HTML::Object::Message;
2              
3 44     44   1145 use strict;
  44         105  
  44         1306  
4              
5 44     44   242 use Carp;
  44         148  
  44         2236  
6 44     44   1103 use Clone::PP;
  44         934  
  44         313  
7 44     44   1969 use Scalar::Util();
  44         133  
  44         1237  
8              
9 44     44   266 use Rose::HTML::Object::Messages qw(CUSTOM_MESSAGE);
  44         132  
  44         369  
10              
11 44     44   321 use base 'Rose::Object';
  44         131  
  44         8737  
12              
13             our $VERSION = '0.606';
14              
15             #our $Debug = 0;
16              
17             use overload
18             (
19 0     0   0 '""' => sub { shift->text },
20 0     0   0 'bool' => sub { 1 },
21 0     0   0 '0+' => sub { 1 },
22 44         620 fallback => 1,
23 44     44   1867 );
  44         1155  
24              
25             use Rose::Object::MakeMethods::Generic
26             (
27 44         543 scalar =>
28             [
29             'id',
30             'variant',
31             ],
32 44     44   5667 );
  44         7859  
33              
34 44     44 0 28994 sub as_string { no warnings 'uninitialized'; "$_[0]" }
  44     12   1139  
  44         26528  
  12         46  
35              
36             sub init
37             {
38 858     858 1 42963 my($self) = shift;
39 858 100       2212 @_ = (text => @_) if(@_ == 1);
40 858         2484 $self->SUPER::init(@_);
41             }
42              
43             sub args
44             {
45 2673     2673 0 9639 my($self) = shift;
46              
47 2673 100       5540 if(@_)
48             {
49 459         752 my %args;
50              
51 459 100 66     2315 if(@_ == 1 && ref $_[0] eq 'ARRAY')
    50 33        
52             {
53 400         697 my $i = 1;
54 400         599 %args = (map { $i++ => $_ } @{$_[0]});
  8         30  
  400         949  
55             }
56             elsif(@_ == 1 && ref $_[0] eq 'HASH')
57             {
58 59         129 %args = %{$_[0]};
  59         317  
59              
60 59         152 my $i = 1;
61              
62 59         288 foreach my $key (sort keys %args)
63             {
64 84 50       371 $args{$i} = $args{$key} unless(exists $args{$i});
65 84         195 $i++;
66             }
67             }
68             else
69             {
70 0         0 my $i = 1;
71 0         0 %args = map { $i++ => $_ } @_;
  0         0  
72             }
73              
74 459         1644 $self->{'args'} = \%args;
75              
76 459 50       1553 return wantarray ? %{$self->{'args'}} : $self->{'args'};
  0         0  
77             }
78              
79 2214 0 100     9847 return wantarray ? %{$self->{'args'} || {}} : ($self->{'args'} ||= {});
  0 50       0  
80             }
81              
82             sub parent
83             {
84 8194     8194 0 15746 my($self) = shift;
85 8194 100       18587 return Scalar::Util::weaken($self->{'parent'} = shift) if(@_);
86 6991         18570 return $self->{'parent'};
87             }
88              
89             sub clone
90             {
91 0     0 0 0 my($self) = shift;
92 0         0 my $clone = Clone::PP::clone($self);
93 0         0 $clone->parent(undef);
94 0         0 return $clone;
95             }
96              
97             sub text
98             {
99 2263     2263 1 7207 my($self) = shift;
100              
101 2263 100       5006 if(@_)
102             {
103 398 50       1577 if(UNIVERSAL::isa($_[0], __PACKAGE__))
104             {
105 0         0 $self->id($_[0]->id);
106 0         0 return $self->{'text'} = $_[0]->text;
107             }
108              
109 398         1618 $self->id(CUSTOM_MESSAGE);
110 398         2299 return $self->{'text'} = $_[0];
111             }
112              
113 1865         11818 return $self->{'text'};
114             }
115              
116 44     44 0 375 sub is_custom { no warnings; shift->id == CUSTOM_MESSAGE }
  44     2230   114  
  44         3306  
  2230         9618  
117              
118             1;
119              
120             __END__
121              
122             =head1 NAME
123              
124             Rose::HTML::Object::Message - Text message object.
125              
126             =head1 SYNOPSIS
127              
128             $msg = Rose::HTML::Object::Message->new('Hello world');
129             print $msg->text; # Hello world
130              
131             =head1 DESCRIPTION
132              
133             L<Rose::HTML::Object::Message> objects encapsulate a text message with an optional integer L<id|/id>.
134              
135             This class inherits from, and follows the conventions of, L<Rose::Object>. See the L<Rose::Object> documentation for more information.
136              
137             =head1 OVERLOADING
138              
139             Stringification is overloaded to call the L<text|/text> method. In numeric and boolean contexts, L<Rose::HTML::Object::Message> objects always evaluate to true.
140              
141             =head1 CONSTRUCTOR
142              
143             =over 4
144              
145             =item B<new [ PARAMS | TEXT ]>
146              
147             Constructs a new L<Rose::HTML::Object::Message> object. If a single argument is passed, it is taken as the value for the L<text|/text> parameter. Otherwise, PARAMS name/value pairs are expected. Any object method is a valid parameter name.
148              
149             =back
150              
151             =head1 OBJECT METHODS
152              
153             =over 4
154              
155             =item B<id [INT]>
156              
157             Get or set the message's integer identifier.
158              
159             =item B<text [ TEXT | OBJECT ]>
160              
161             Get or set the message's text. If the message text is set to a TEXT string (rather than a L<Rose::HTML::Object::Message>-derived OBJECT), the L<id|/id> is set to the value of the constant C<Rose::HTML::Object::Message::CUSTOM_MESSAGE>.
162              
163             =back
164              
165             =head1 AUTHOR
166              
167             John C. Siracusa (siracusa@gmail.com)
168              
169             =head1 LICENSE
170              
171             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.