File Coverage

blib/lib/Rose/ObjectX/CAF/MethodMaker.pm
Criterion Covered Total %
statement 28 40 70.0
branch 10 26 38.4
condition 5 14 35.7
subroutine 7 10 70.0
pod 1 1 100.0
total 51 91 56.0


line stmt bran cond sub pod time code
1             package Rose::ObjectX::CAF::MethodMaker;
2 2     2   9 use warnings;
  2         5  
  2         56  
3 2     2   13 use strict;
  2         3  
  2         56  
4 2     2   11 use Carp;
  2         3  
  2         123  
5 2     2   10 use base qw( Rose::Object::MakeMethods::Generic );
  2         4  
  2         2237  
6             my $Debug = 0; #$ENV{PERL_DEBUG};
7              
8             our $VERSION = '0.03';
9              
10             =head1 NAME
11              
12             Rose::ObjectX::CAF::MethodMaker - Class::Accessor::Fast compatability for Rose::Object
13              
14             =head1 SYNOPSIS
15              
16             # see Rose::ObjectX::CAF
17            
18             =head1 DESCRIPTION
19              
20             This is a subclass of Rose::Object::MakeMethods::Generic. See those docs
21             and those of Rose::ObjectX::CAF.
22              
23             =head1 METHODS
24              
25             =head2 scalar
26              
27             Overrides the Rose::Object::MakeMethods::Generic method of the same name
28             to provide read-only accessors like mk_ro_accessors().
29              
30             =cut
31              
32             # extend for mk_ro_accessors support
33             sub scalar {
34 2     2 1 303 my ( $class, $name, $args ) = @_;
35              
36 2         3 my %methods;
37              
38 2   33     15 my $key = $args->{'hash_key'} || $name;
39 2   100     13 my $interface = $args->{'interface'} || 'get_set';
40              
41 2 50       12 if ( $interface eq 'get_set_init' ) {
    100          
    50          
42 0   0     0 my $init_method = $args->{'init_method'} || "init_$name";
43              
44             $methods{$name} = sub {
45 0 0   0   0 return $_[0]->{$key} = $_[1] if ( @_ > 1 );
46              
47 0 0       0 return defined $_[0]->{$key}
48             ? $_[0]->{$key}
49             : ( $_[0]->{$key} = $_[0]->$init_method() );
50 0         0 };
51             }
52             elsif ( $interface eq 'get_set' ) {
53 1 50 33     13 if ( $Rose::Object::MakeMethods::Generic::Have_CXSA
54             && !$ENV{'ROSE_OBJECT_NO_CLASS_XSACCESOR'} )
55             {
56             $methods{$name} = {
57             make_method => sub {
58 1     1   32 my ( $name, $target_class, $options ) = @_;
59              
60 1 50       4 $Debug
61             && warn
62             "Class::XSAccessor make method ($name => $key) in $target_class\n";
63              
64 1 50       11 Class::XSAccessor->import(
65             accessors => { $name => $key },
66             class => $target_class,
67             replace => $options->{'override_existing'} ? 1 : 0
68             );
69             },
70 1         9 };
71             }
72             else {
73             $methods{$name} = sub {
74 0 0   0   0 return $_[0]->{$key} = $_[1] if ( @_ > 1 );
75 0         0 return $_[0]->{$key};
76 0         0 };
77             }
78             }
79             elsif ( $interface eq 'ro' ) {
80 1 50 33     8 if ( $Rose::Object::MakeMethods::Generic::Have_CXSA
81             && !$ENV{'ROSE_OBJECT_NO_CLASS_XSACCESOR'} )
82             {
83             $methods{$name} = {
84             make_method => sub {
85 1     1   320 my ( $name, $target_class, $options ) = @_;
86              
87 1 50       4 $Debug
88             && warn
89             "Class::XSAccessor make method ($name => $key) in $target_class\n";
90              
91 1 50       10 Class::XSAccessor->import(
92             getters => { $name => $key },
93             class => $target_class,
94             replace => $options->{'override_existing'} ? 1 : 0
95             );
96             },
97 1         8 };
98             }
99             else {
100             $methods{$name} = sub {
101 0 0   0   0 if ( @_ > 1 ) {
102 0         0 croak "usage: $name() is read-only (getter not setter)";
103             }
104 0         0 return $_[0]->{$key};
105 0         0 };
106             }
107             }
108 0         0 else { Carp::croak "Unknown interface: $interface" }
109              
110 2         8 return \%methods;
111             }
112              
113             =head1 AUTHOR
114              
115             Peter Karman, C<< >>
116              
117             =head1 BUGS
118              
119             Please report any bugs or feature requests to C, or through
120             the web interface at L. I will be notified, and then you'll
121             automatically be notified of progress on your bug as I make changes.
122              
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc Rose::ObjectX::CAF
129              
130              
131             You can also look for information at:
132              
133             =over 4
134              
135             =item * RT: CPAN's request tracker
136              
137             L
138              
139             =item * AnnoCPAN: Annotated CPAN documentation
140              
141             L
142              
143             =item * CPAN Ratings
144              
145             L
146              
147             =item * Search CPAN
148              
149             L
150              
151             =back
152              
153              
154             =head1 ACKNOWLEDGEMENTS
155              
156              
157             =head1 COPYRIGHT & LICENSE
158              
159             Copyright 2009 Peter Karman.
160              
161             This program is free software; you can redistribute it and/or modify it
162             under the terms of either: the GNU General Public License as published
163             by the Free Software Foundation; or the Artistic License.
164              
165             See http://dev.perl.org/licenses/ for more information.
166              
167              
168             =cut
169              
170             1; # End of Rose::ObjectX::CAF