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   1285 use strict;
  44         102  
  44         1655  
4              
5 44     44   421 use Carp;
  44         84  
  44         2983  
6 44     44   912 use Clone::PP;
  44         1303  
  44         496  
7 44     44   2427 use Scalar::Util();
  44         97  
  44         1606  
8              
9 44     44   231 use Rose::HTML::Object::Messages qw(CUSTOM_MESSAGE);
  44         106  
  44         433  
10              
11 44     44   317 use base 'Rose::Object';
  44         161  
  44         10287  
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         701 fallback => 1,
23 44     44   1559 );
  44         2251  
24              
25             use Rose::Object::MakeMethods::Generic
26             (
27 44         902 scalar =>
28             [
29             'id',
30             'variant',
31             ],
32 44     44   5828 );
  44         9722  
33              
34 44     44 0 31879 sub as_string { no warnings 'uninitialized'; "$_[0]" }
  44     12   167  
  44         31443  
  12         65  
35              
36             sub init
37             {
38 859     859 1 289170 my($self) = shift;
39 859 100       2658 @_ = (text => @_) if(@_ == 1);
40 859         3215 $self->SUPER::init(@_);
41             }
42              
43             sub args
44             {
45 2677     2677 0 10915 my($self) = shift;
46              
47 2677 100       5459 if(@_)
48             {
49 459         896 my %args;
50              
51 459 100 66     2891 if(@_ == 1 && ref $_[0] eq 'ARRAY')
    50 33        
52             {
53 400         782 my $i = 1;
54 400         669 %args = (map { $i++ => $_ } @{$_[0]});
  8         31  
  400         1262  
55             }
56             elsif(@_ == 1 && ref $_[0] eq 'HASH')
57             {
58 59         148 %args = %{$_[0]};
  59         270  
59              
60 59         141 my $i = 1;
61              
62 59         267 foreach my $key (sort keys %args)
63             {
64 84 50       521 $args{$i} = $args{$key} unless(exists $args{$i});
65 84         200 $i++;
66             }
67             }
68             else
69             {
70 0         0 my $i = 1;
71 0         0 %args = map { $i++ => $_ } @_;
  0         0  
72             }
73              
74 459         1884 $self->{'args'} = \%args;
75              
76 459 50       2002 return wantarray ? %{$self->{'args'}} : $self->{'args'};
  0         0  
77             }
78              
79 2218 0 100     10068 return wantarray ? %{$self->{'args'} || {}} : ($self->{'args'} ||= {});
  0 50       0  
80             }
81              
82             sub parent
83             {
84 8207     8207 0 15342 my($self) = shift;
85 8207 100       18506 return Scalar::Util::weaken($self->{'parent'} = shift) if(@_);
86 7003         18481 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 2268     2268 1 7333 my($self) = shift;
100              
101 2268 100       4599 if(@_)
102             {
103 399 50       1973 if(UNIVERSAL::isa($_[0], __PACKAGE__))
104             {
105 0         0 $self->id($_[0]->id);
106 0         0 return $self->{'text'} = $_[0]->text;
107             }
108              
109 399         1937 $self->id(CUSTOM_MESSAGE);
110 399         2921 return $self->{'text'} = $_[0];
111             }
112              
113 1869         12676 return $self->{'text'};
114             }
115              
116 44     44 0 398 sub is_custom { no warnings; shift->id == CUSTOM_MESSAGE }
  44     2234   187  
  44         3976  
  2234         8679  
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.