File Coverage

blib/lib/Rstats/Class.pm
Criterion Covered Total %
statement 88 88 100.0
branch 15 20 75.0
condition 1 3 33.3
subroutine 32 33 96.9
pod 1 3 33.3
total 137 147 93.2


line stmt bran cond sub pod time code
1             package Rstats::Class;
2              
3 21     584   17181 use Object::Simple -base;
  21         29210  
  21         169  
4             require Rstats::Func;
5 21     21   2044 use Carp 'croak';
  21         48  
  21         1483  
6 21     21   10951 use Rstats::Util ();
  21         67  
  21         619  
7 21     21   116 use Digest::MD5 'md5_hex';
  21         45  
  21         1484  
8 21     21   11226 use Rstats::Object;
  21         55  
  21         141  
9              
10             has helpers => sub { {} };
11              
12             sub get_helper {
13 128966     128966 0 218633 my ($self, $name,) = @_;
14            
15 128966 100       2802840 if ($self->{proxy}{$name}) {
    100          
16 628         3457 return bless {r => $self}, $self->{proxy}{$name};
17             }
18             elsif (my $h = $self->helpers->{$name}) {
19 128315         1278049 return $h;
20             }
21              
22 23         197 my $found;
23 23         233 my $class = 'Rstats::Helpers::' . md5_hex "$name:$self";
24 23 50       775 my $re = $name eq '' ? qr/^(([^.]+))/ : qr/^(\Q$name\E\.([^.]+))/;
25 23         63 for my $key (keys %{$self->helpers}) {
  23         554  
26 4646 100       14835 $key =~ $re ? ($found, my $method) = (1, $2) : next;
27 329         779 my $sub = $self->get_helper($1);
28             Rstats::Util::monkey_patch $class, $method => sub {
29 627     627   860 my $proxy = shift;
        627      
        627      
        1190      
        1178      
        627      
        1178      
        615      
        615      
        615      
        627      
        1190      
        1190      
        1190      
        1178      
        615      
        615      
        627      
        1178      
        627      
        9      
30 627 100       979 return $sub->($proxy->{r}, @{$proxy->{args} || []}, @_);
  627         12110  
31             }
32 329         1846 }
33              
34 23 50       318 $found ? push @{$self->{namespaces}}, $class : return undef;
  23         83  
35 23         68 $self->{proxy}{$name} = $class;
36 23         81 return $self->get_helper($name);
37             }
38              
39             # TODO
40             # logp1x
41             # gamma
42             # lgamma
43             # complete_cases
44             # cor
45             # pmatch regexpr
46             # substr substring
47             # strsplit strwrap
48             # outer(x, y, f)
49             # reorder()
50             # relevel()
51             # read.csv()
52             # read.csv2()
53             # read.delim()
54             # read.delim2()
55             # read.fwf()
56             # merge
57             # replicate
58             # split
59             # by
60             # aggregate
61             # reshape
62              
63             my @func_names = qw/
64             sd
65             sin
66             sweep
67             set_seed
68             runif
69             apply
70             mapply
71             tapply
72             lapply
73             sapply
74             abs
75             acos
76             acosh
77             append
78             Arg
79             asin
80             asinh
81             atan
82             atanh
83             atan2
84             c_
85             c_double
86             c_character
87             c_complex
88             c_integer
89             c_logical
90             C_
91             charmatch
92             chartr
93             cbind
94             ceiling
95             col
96             colMeans
97             colSums
98             Conj
99             cos
100             cosh
101             cummax
102             cummin
103             cumsum
104             cumprod
105             data_frame
106             diag
107             diff
108             exp
109             expm1
110             factor
111             F
112             F_
113             FALSE
114             floor
115             gl
116             grep
117             gsub
118             head
119             i_
120             ifelse
121             interaction
122             I
123             Im
124             Re
125             intersect
126             kronecker
127             list
128             log
129             logb
130             log2
131             log10
132             lower_tri
133             match
134             median
135             merge
136             Mod
137             NA
138             NaN
139             na_omit
140             ncol
141             nrow
142             NULL
143             numeric
144             matrix
145             max
146             mean
147             min
148             nchar
149             order
150             ordered
151             outer
152             paste
153             pi
154             pmax
155             pmin
156             prod
157             range
158             rank
159             rbind
160             quantile
161             rep
162             replace
163             rev
164             rnorm
165             round
166             row
167             rowMeans
168             rowSums
169             sample
170             seq
171             sequence
172             set_diag
173             setdiff
174             setequal
175             sinh
176             sum
177             sqrt
178             sort
179             sub
180             subset
181             t
182             tail
183             tan
184             tanh
185             tolower
186             toupper
187             T_
188             TRUE
189             transform
190             trunc
191             unique
192             union
193             upper_tri
194             var
195             which
196             labels
197             levels
198             names
199             nlevels
200             dimnames
201             colnames
202             rownames
203             mode
204             str
205             typeof
206             pi
207             complex
208             array
209             length
210             clone
211             equal
212             not_equal
213             less_than
214             less_than_or_equal
215             more_than
216             more_than_or_equal
217             add
218             subtract
219             multiply
220             divide
221             pow
222             negate
223             dim
224             Inf
225             NaN
226             NA
227             to_string
228             get
229             set
230             getin
231             value
232             values
233             dim_as_array
234             class
235             type
236             get_type
237             at
238             get_length
239             /;
240              
241             sub new {
242 21     584 1 185 my $self = shift->SUPER::new(@_);
243            
244 21         208 for my $func_name (@func_names) {
245 21     21   10783 no strict 'refs';
  21         52  
  21         1450  
246 3675         4504 my $func = \&{"Rstats::Func::$func_name"};
  3675         10988  
247 3675         7773 $self->helper($func_name => $func);
248             }
249              
250 21     21   101 no strict 'refs';
  21         40  
  21         13565  
251 21         93 $self->helper('is.array' => \&Rstats::Func::is_array);
252 21         77 $self->helper('is.character' => \&Rstats::Func::is_character);
253 21         74 $self->helper('is.complex' => \&Rstats::Func::is_complex);
254 21         85 $self->helper('is.finite' => \&Rstats::Func::is_finite);
255 21         79 $self->helper('is.infinite' => \&Rstats::Func::is_infinite);
256 21         82 $self->helper('is.list' => \&Rstats::Func::is_list);
257 21         85 $self->helper('is.matrix' => \&Rstats::Func::is_matrix);
258 21         83 $self->helper('is.na' => \&Rstats::Func::is_na);
259 21         76 $self->helper('is.nan' => \&Rstats::Func::is_nan);
260 21         80 $self->helper('is.null' => \&Rstats::Func::is_null);
261 21         85 $self->helper('is.numeric' => \&Rstats::Func::is_numeric);
262 21         80 $self->helper('is.double' => \&Rstats::Func::is_double);
263 21         81 $self->helper('is.integer' => \&Rstats::Func::is_integer);
264 21         87 $self->helper('is.vector' => \&Rstats::Func::is_vector);
265 21         83 $self->helper('is.factor' => \&Rstats::Func::is_factor);
266 21         85 $self->helper('is.ordered' => \&Rstats::Func::is_ordered);
267 21         78 $self->helper('is.data_frame' => \&Rstats::Func::is_data_frame);
268 21         174 $self->helper('is.logical' => \&Rstats::Func::is_logical);
269 21         83 $self->helper('is.element' => \&Rstats::Func::is_element);
270              
271 21         83 $self->helper('as.array' => \&Rstats::Func::as_array);
272 21         80 $self->helper('as.character' => \&Rstats::Func::as_character);
273 21         77 $self->helper('as.complex' => \&Rstats::Func::as_complex);
274 21         77 $self->helper('as.integer' => \&Rstats::Func::as_integer);
275 21         77 $self->helper('as.double' => \&Rstats::Func::as_double);
276 21         80 $self->helper('as.list' => \&Rstats::Func::as_list);
277 21         80 $self->helper('as.logical' => \&Rstats::Func::as_logical);
278 21         80 $self->helper('as.matrix' => \&Rstats::Func::as_matrix);
279 21         73 $self->helper('as.numeric' => \&Rstats::Func::as_numeric);
280 21         77 $self->helper('as.vector' => \&Rstats::Func::as_vector);
281              
282 21         83 $self->helper('read.table' => \&Rstats::Func::read_table);
283              
284 21         65 return $self;
285             }
286              
287             sub AUTOLOAD {
288 1339     1339   2182 my $self = shift;
289              
290 1339         7747 my ($package, $method) = split /::(\w+)$/, our $AUTOLOAD;
291 1339 50 33     11164 Carp::croak "Undefined subroutine &${package}::$method called"
292             unless Scalar::Util::blessed $self && $self->isa(__PACKAGE__);
293              
294             # Call helper with current controller
295 1339 50       3676 Carp::croak qq{Can't locate object method "$method" via package "$package"}
296             unless my $helper = $self->get_helper($method);
297            
298             # Helper
299 1339 100       3694 if (ref $helper eq 'CODE') {
300 719         48422 return $helper->($self, @_);
301             }
302             #Proxy
303             else {
304 620         2510 return $helper;
305             }
306             }
307              
308       0     sub DESTROY { }
309              
310             sub helper {
311 4305     4305 0 5761 my $self = shift;
312            
313             # Merge
314 4305 50       13147 my $helpers = ref $_[0] eq 'HASH' ? $_[0] : {@_};
315 4305         5525 $self->helpers({%{$self->helpers}, %$helpers});
  4305         90790  
316            
317 4305         237200 return $self;
318             }
319              
320             1;
321              
322             =head1 NAME
323              
324             Rstats::Class - Rstats Object-Oriented interface
325              
326             =head1 SYNOPSYS
327            
328             use Rstats::Class;
329             my $r = Rstats::Class->new;
330            
331             # Array
332             my $v1 = $r->c_(1, 2, 3);
333             my $v2 = $r->c_(2, 3, 4);
334             my $v3 = $v1 + v2;
335             print $v3;