File Coverage

blib/lib/Set/Associate/NewKey.pm
Criterion Covered Total %
statement 35 38 92.1
branch 8 10 80.0
condition n/a
subroutine 12 13 92.3
pod 5 5 100.0
total 60 66 90.9


line stmt bran cond sub pod time code
1 11     11   6232 use 5.006;
  11         30  
2 11     11   62 use strict;
  11         17  
  11         274  
3 11     11   45 use warnings;
  11         16  
  11         744  
4              
5             package Set::Associate::NewKey;
6              
7             # ABSTRACT: New Key assignment methods
8              
9             our $VERSION = '0.004001';
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 11     11   53 use Carp qw( croak );
  11         16  
  11         736  
14 11     11   663 use Moose qw( has with );
  11         351567  
  11         76  
15 11     11   50937 use MooseX::AttributeShortcuts;
  11         298450  
  11         93  
16              
17 11     11   59357 use Set::Associate::Utils qw( _warn_nonmethod );
  11         20  
  11         1323  
18              
19              
20              
21              
22              
23              
24              
25              
26              
27             has name => (
28             isa => Str =>,
29             is => rwp =>,
30             required => 1,
31             );
32              
33              
34              
35              
36              
37              
38              
39              
40              
41             has code => (
42             isa => CodeRef =>,
43             is => rwp =>,
44             required => 1,
45             traits => ['Code'],
46             handles => {
47             get_assoc => execute_method =>,
48             },
49             );
50              
51             with 'Set::Associate::Role::NewKey' => { can_get_assoc => 1, };
52              
53             __PACKAGE__->meta->make_immutable;
54 11     11   57 no Moose;
  11         14  
  11         64  
55              
56              
57              
58              
59              
60              
61              
62              
63              
64              
65              
66              
67              
68              
69              
70              
71              
72              
73             sub linear_wrap {
74 3 100   3 1 97 shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'linear_wrap' );
75 3         1303 require Set::Associate::NewKey::LinearWrap;
76 3         93 return Set::Associate::NewKey::LinearWrap->new(@_);
77             }
78              
79              
80              
81              
82              
83              
84              
85              
86              
87              
88              
89              
90              
91              
92              
93              
94              
95             sub random_pick {
96 2 100   2 1 129 shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'random_pick' );
97 2         1330 require Set::Associate::NewKey::RandomPick;
98 2         89 return Set::Associate::NewKey::RandomPick->new(@_);
99             }
100              
101              
102              
103              
104              
105              
106              
107              
108              
109              
110              
111              
112              
113              
114              
115              
116              
117              
118              
119              
120              
121              
122              
123             sub pick_offset {
124 0 0   0 1 0 shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'pick_offset' );
125 0         0 require Set::Associate::NewKey::PickOffset;
126 0         0 return Set::Associate::NewKey::PickOffset->new(@_);
127             }
128              
129              
130              
131              
132              
133              
134              
135              
136              
137              
138              
139              
140              
141              
142              
143              
144              
145              
146              
147             sub hash_sha1 {
148 2 100   2 1 112 shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'hash_sha1' );
149 2         1082 require Set::Associate::NewKey::HashSHA1;
150 2         199 return Set::Associate::NewKey::HashSHA1->new(@_);
151             }
152              
153              
154              
155              
156              
157              
158              
159              
160              
161              
162              
163              
164              
165              
166              
167              
168              
169              
170              
171             sub hash_md5 {
172 2 100   2 1 95 shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'hash_md5' );
173 2         1045 require Set::Associate::NewKey::HashMD5;
174 2         143 return Set::Associate::NewKey::HashMD5->new(@_);
175             }
176              
177             1;
178              
179             __END__
180              
181             =pod
182              
183             =encoding UTF-8
184              
185             =head1 NAME
186              
187             Set::Associate::NewKey - New Key assignment methods
188              
189             =head1 VERSION
190              
191             version 0.004001
192              
193             =head1 DESCRIPTION
194              
195             This class implements the mechanism which controls how the values are assigned to 'new' keys.
196              
197             The part you're mostly interested in are the L</CLASS METHODS>, which return the right assignment method.
198              
199             This is more or less a wrapper for passing around subs with an implicit interface.
200              
201             my $assigner = Set::Associate::NewKey->new(
202             name => 'linear_wrap',
203             code => sub {
204             my ( $self, $sa , $key ) = @_;
205             ....
206             },
207             );
208              
209             my $value = $assigner->run( $set_associate_object, $key );
210              
211             =head1 CONSTRUCTOR ARGUMENTS
212              
213             =head2 name
214              
215             required Str
216              
217             =head2 code
218              
219             required CodeRef
220              
221             =head1 CLASS METHODS
222              
223             =head2 linear_wrap
224              
225             C<shift>'s the first item off the internal C<_items_cache>
226              
227             my $sa = Set::Associate->new(
228             ...
229             on_new_key => Set::Associate::NewKey->linear_wrap
230             );
231              
232             or alternatively
233              
234             my $code = Set::Associate::NewKey->linear_wrap
235             my $newval = $code->run( $set, $key_which_will_be_ignored );
236              
237             =head2 random_pick
238              
239             non-destructively picks an element from C<_items_cache> at random.
240              
241             my $sa = Set::Associate->new(
242             ...
243             on_new_key => Set::Associate::NewKey->random_pick
244             );
245              
246             or alternatively
247              
248             my $code = Set::Associate::NewKey->random_pick
249             my $newval = $code->run( $set, $key_which_will_be_ignored );
250              
251             =head2 pick_offset
252              
253             Assuming offset is numeric, pick either that number, or a modulo of that number.
254              
255             B<NOTE:> do not use this unless you are only working with numeric keys.
256              
257             If you're using anything else, the hash_sha1 or hash_md5 methods are suggested.
258              
259             my $sa = Set::Associate->new(
260             ...
261             on_new_key => Set::Associate::NewKey->pick_offset
262             );
263              
264             or alternatively
265              
266             my $code = Set::Associate::NewKey->pick_offset
267             my $newval = $code->run( $set, 9001 ); # despite picking numbers OVER NINE THOUSAND
268             # will still return items in the array
269              
270             =head2 hash_sha1
271              
272             B<requires C<bigint> support>
273              
274             Determines the offset for L</pick_offset> from taking the numeric value of the C<SHA1> hash of the given string
275              
276             my $sa = Set::Associate->new(
277             ...
278             on_new_key => Set::Associate::NewKey->hash_sha1
279             );
280              
281             or alternatively
282              
283             my $code = Set::Associate::NewKey->hash_sha1();
284             my $newval = $code->run( $set, "Some String" );
285              
286             =head2 hash_md5
287              
288             B<requires C<bigint> support>
289              
290             Determines the offset for L</pick_offset> from taking the numeric value of the MD5 hash of the given string
291              
292             my $sa = Set::Associate->new(
293             ...
294             on_new_key => Set::Associate::NewKey->hash_md5
295             );
296              
297             or alternatively
298              
299             my $code = Set::Associate::NewKey->hash_md5();
300             my $newval = $code->run( $set, "Some String" );
301              
302             =head1 ATTRIBUTES
303              
304             =head2 name
305              
306             =head2 code
307              
308             =head1 AUTHOR
309              
310             Kent Fredric <kentnl@cpan.org>
311              
312             =head1 COPYRIGHT AND LICENSE
313              
314             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
315              
316             This is free software; you can redistribute it and/or modify it under
317             the same terms as the Perl 5 programming language system itself.
318              
319             =cut