File Coverage

blib/lib/Rose/ObjectX/CAF.pm
Criterion Covered Total %
statement 30 31 96.7
branch 3 4 75.0
condition n/a
subroutine 9 9 100.0
pod 4 4 100.0
total 46 48 95.8


line stmt bran cond sub pod time code
1             package Rose::ObjectX::CAF;
2 2     2   30022 use warnings;
  2         5  
  2         72  
3 2     2   12 use strict;
  2         4  
  2         78  
4 2     2   12 use base qw( Rose::Object );
  2         8  
  2         2010  
5 2     2   463 use Carp;
  2         4  
  2         156  
6 2     2   1227 use Rose::ObjectX::CAF::MethodMaker;
  2         830  
  2         32  
7              
8             our $VERSION = '0.03';
9              
10             =head1 NAME
11              
12             Rose::ObjectX::CAF - Class::Accessor::Fast compatability for Rose::Object
13              
14             =head1 SYNOPSIS
15              
16             package MyClass;
17             use strict;
18             use base qw( Rose::ObjectX::CAF );
19             __PACKAGE__->mk_accessors(qw( foo bar ));
20             __PACKAGE__->mk_ro_accessors(qw( color name ));
21             1;
22            
23             =head1 DESCRIPTION
24              
25             Rose::ObjectX::CAF is a compatability layer for Class::Accessor::Fast users who
26             want to migrate to Rose::Object.
27              
28             As evidenced in L,
29             L + L is
30             much faster than Class::Accessor::Fast (and more extensible).
31             I decided to switch over, but had a lot of code already using CAF.
32             So this class was born to make the migration easier.
33              
34             Just replace this line in your classes:
35              
36             use base qw( Class::Accessor::Fast );
37              
38             with this:
39              
40             use base qw( Rose::ObjectX::CAF );
41              
42             and no other changes should be necessary.
43              
44             =head1 METHODS
45              
46             =head2 new
47              
48             Works like CAF, but may take a hash (the Rose::Object style)
49             or hash ref (the CAF style).
50              
51             =cut
52              
53             sub new {
54 2     2 1 188 my $class = shift;
55 2         6 my $self = bless {}, $class;
56 2 100       11 $self->init( @_ > 1 ? @_ : %{ $_[0] } );
  1         4  
57 2         9 return $self;
58             }
59              
60             =head2 init
61              
62             Like Rose::Object, called by new(). Do not override new() in your
63             subclasses; override init() instead. And be sure to call:
64              
65             $self->SUPER::init(@_); # or with MRO::Compat, $self->next::method(@_);
66              
67             in your subclass.
68              
69             Rather than calling
70             the method name for each param passed in new(), the value
71             is simply set in the object as a hash ref. This assumes
72             every object is a blessed hash ref.
73              
74             The reason the hash is preferred over the method call
75             is to support read-only accessors, which will croak
76             if init() tried to set values with them.
77              
78             =cut
79              
80             sub init {
81 2     2 1 4 my $self = shift;
82              
83             # assume object is hash and set key
84             # rather than call method, since we have read-only methods.
85 2         6 while (@_) {
86 2         3 my $method = shift;
87 2 50       25 if (!$self->can($method)) {
88 0         0 croak "No such method $method";
89             }
90 2         14 $self->{$method} = shift;
91             }
92              
93 2         3 return $self;
94             }
95              
96             =head2 mk_accessors( I<@list_of_method_names> )
97              
98             Just like CAF.
99              
100             =cut
101              
102             sub mk_accessors {
103 1     1 1 689 my $class = shift;
104 1         14 Rose::ObjectX::CAF::MethodMaker->make_methods(
105             { target_class => $class, },
106             scalar => \@_ );
107             }
108              
109             =head2 mk_ro_accessors( I<@list_of_method_names> );
110              
111             Just like CAF, for read-only (accessor/getter) methods.
112              
113             =cut
114              
115             sub mk_ro_accessors {
116 1     1 1 216 my $class = shift;
117 1         6 Rose::ObjectX::CAF::MethodMaker->make_methods(
118             { target_class => $class, },
119             'scalar --ro' => \@_ );
120             }
121              
122             =head1 AUTHOR
123              
124             Peter Karman, C<< >>
125              
126             =head1 BUGS
127              
128             Please report any bugs or feature requests to C, or through
129             the web interface at L. I will be notified, and then you'll
130             automatically be notified of progress on your bug as I make changes.
131              
132              
133             =head1 SUPPORT
134              
135             You can find documentation for this module with the perldoc command.
136              
137             perldoc Rose::ObjectX::CAF
138              
139              
140             You can also look for information at:
141              
142             =over 4
143              
144             =item * RT: CPAN's request tracker
145              
146             L
147              
148             =item * AnnoCPAN: Annotated CPAN documentation
149              
150             L
151              
152             =item * CPAN Ratings
153              
154             L
155              
156             =item * Search CPAN
157              
158             L
159              
160             =back
161              
162              
163             =head1 ACKNOWLEDGEMENTS
164              
165              
166             =head1 COPYRIGHT & LICENSE
167              
168             Copyright 2009 Peter Karman.
169              
170             This program is free software; you can redistribute it and/or modify it
171             under the terms of either: the GNU General Public License as published
172             by the Free Software Foundation; or the Artistic License.
173              
174             See http://dev.perl.org/licenses/ for more information.
175              
176              
177             =cut
178              
179             1; # End of Rose::ObjectX::CAF