| 1 | 11 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Set::Associate::NewKey; | ||||||||||
| 6 | |||||||||||
| 7 | # ABSTRACT: New Key assignment methods | ||||||||||
| 8 | |||||||||||
| 9 | 11 | our $VERSION = '0.004000'; | |||||||||
| 10 | |||||||||||
| 11 | 11 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||
| 12 | |||||||||||
| 13 | 11 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 11 | ▶ | use Moose qw( has with ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 11 | ▶ | use MooseX::AttributeShortcuts; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 11 | ▶ | use Set::Associate::Utils qw( _warn_nonmethod ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | 11 | has name => ( | |||||||||
| 28 | isa => Str =>, | ||||||||||
| 29 | is => rwp =>, | ||||||||||
| 30 | required => 1, | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 11 | 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 | 11 | with 'Set::Associate::Role::NewKey' => { can_get_assoc => 1, }; | |||||||||
| 52 | |||||||||||
| 53 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||
| 54 | 11 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | |||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | |||||||||||
| 73 | sub linear_wrap { | ||||||||||
| 74 | 3 | ▶ | shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'linear_wrap' ); | ||||||||
|
Subroutine
linear_wrap: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 75 | 3 | require Set::Associate::NewKey::LinearWrap; | |||||||||
| 76 | 3 | 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 | ▶ | shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'random_pick' ); | ||||||||
|
Subroutine
random_pick: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 97 | 2 | require Set::Associate::NewKey::RandomPick; | |||||||||
| 98 | 2 | 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 | ▶ | shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'pick_offset' ); | ||||||||
|
Subroutine
pick_offset: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 125 | 0 | require Set::Associate::NewKey::PickOffset; | |||||||||
| 126 | 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 | ▶ | shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'hash_sha1' ); | ||||||||
|
Subroutine
hash_sha1: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 149 | 2 | require Set::Associate::NewKey::HashSHA1; | |||||||||
| 150 | 2 | 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 | ▶ | shift @_ unless _warn_nonmethod( $_[0], __PACKAGE__, 'hash_md5' ); | ||||||||
|
Subroutine
hash_md5: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 173 | 2 | require Set::Associate::NewKey::HashMD5; | |||||||||
| 174 | 2 | return Set::Associate::NewKey::HashMD5->new(@_); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 11 | 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.004000 | ||||||||||
| 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) 2015 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 | ||||||||||