diff options
author | Gianni Ceccarelli <dakkar@thenautilus.net> | 2011-11-14 14:17:22 +0000 |
---|---|---|
committer | Gianni Ceccarelli <dakkar@thenautilus.net> | 2011-11-14 14:17:22 +0000 |
commit | bb0870231cd8f7caee0c6ccf6b455919f9e0a79b (patch) | |
tree | 1edf571226d5c767dc236e185064642fcd1ba383 | |
parent | Build results of 467399a (on master) (diff) | |
parent | fix inserting range w/o overlap (diff) | |
download | data-multivalued-bb0870231cd8f7caee0c6ccf6b455919f9e0a79b.tar.gz data-multivalued-bb0870231cd8f7caee0c6ccf6b455919f9e0a79b.tar.bz2 data-multivalued-bb0870231cd8f7caee0c6ccf6b455919f9e0a79b.zip |
Build results of dd15e2e (on master)
-rw-r--r-- | Changes | 2 | ||||
-rw-r--r-- | MANIFEST | 1 | ||||
-rw-r--r-- | lib/Data/MultiValued.pod | 98 | ||||
-rw-r--r-- | lib/Data/MultiValued/RangeContainer.pm | 8 | ||||
-rw-r--r-- | t/ranges-setting.t | 12 |
5 files changed, 118 insertions, 3 deletions
@@ -1,4 +1,4 @@ Revision history for Data::MultiValued -0.0.1 2011-11-10 15:18:06 Europe/London +0.0.1 2011-11-14 14:17:16 Europe/London - first working version @@ -4,6 +4,7 @@ META.json META.yml Makefile.PL dist.ini +lib/Data/MultiValued.pod lib/Data/MultiValued/AttributeAccessors.pm lib/Data/MultiValued/AttributeTrait.pm lib/Data/MultiValued/AttributeTrait/Ranges.pm diff --git a/lib/Data/MultiValued.pod b/lib/Data/MultiValued.pod new file mode 100644 index 0000000..bfc49f9 --- /dev/null +++ b/lib/Data/MultiValued.pod @@ -0,0 +1,98 @@ +# PODNAME: Data::MultiValued + + +__END__ +=pod + +=head1 NAME + +Data::MultiValued + +=head1 VERSION + +version 0.0.1 + +=head1 SYNOPSIS + + use Data::MultiValued::Tags; + + my $obj = Data::MultiValued::Tags->new(); + $obj->set({ + tag => 'tag1', + value => 'a string', + }); + say $obj->get({tag=>'tag1'}); # prints 'a string' + say $obj->get({tag=>'tag2'}); # dies + +Also: + + package My::Class; + use Moose; + use Data::MultiValued::AttributeTrait::Tags; + + has stuff => ( + is => 'rw', + isa => 'Int', + traits => ['MultiValued::Tags'], + ); + + # later + + my $obj = My::Class->new(); + $obj->stuff_multi({tag=>'tag1'},123); + say $obj->stuff_multi({tag=>'tag1'}); # prints 123 + +=head1 DESCRIPTION + +This set of classes allows you to store different values inside a +single object, and access them by tag and / or by a numeric value. + +Yes, you could do the same with hashes and some clever use of +arrays. Or you could use L<Array::IntSpan>. Or some other CPAN +module. Why use these? + +=over 4 + +=item * + +they are optimised for serialisation, see +L<Data::MultiValued::UglySerializationHelperRole> and F<t/json.t>. + +=item * + +you get accessors generated for your Moose attributes just by setting +a trait + +=item * + +tags and ranges interact in sensible ways, including clearing ranges + +=back + +=head1 NAME + +Data::MultiValued - store tag- and range-dependant data in a scalar or Moose attribute + +=head1 Where to go from here + +Look at the tests for detailed examples of usage. Look at +L<Data::MultiValued::Tags>, L<Data::MultiValued::Ranges> and +L<Data::MultiValued::TagsAndRanges> for the containers +themselves. Look at L<Data::MultiValued::AttributeTrait::Tags>, +L<Data::MultiValued::AttributeTrait::Ranges> and +L<Data::MultiValued::AttributeTrait::TagsAndRanges> for the Moose +attribute traits. + +=head1 AUTHOR + +Gianni Ceccarelli <dakkar@thenautilus.net> + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2011 by Net-a-porter.com. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut + diff --git a/lib/Data/MultiValued/RangeContainer.pm b/lib/Data/MultiValued/RangeContainer.pm index e4c459f..69dcc38 100644 --- a/lib/Data/MultiValued/RangeContainer.pm +++ b/lib/Data/MultiValued/RangeContainer.pm @@ -160,13 +160,14 @@ sub _splice_slot { # by costruction, the first and the last may have to be split, all # others must be removed - my $first_to_replace = $overlap->[0], - my $last_to_replace = $overlap->[-1], + my $first_to_replace; my $how_many = @$overlap; my @replacement = $new ? ($new) : (); if ($how_many > 0) { # we have to splice + $first_to_replace = $overlap->[0]; + my $last_to_replace = $overlap->[-1]; my $first = $self->_storage->[$first_to_replace]; my $last = $self->_storage->[$last_to_replace]; @@ -187,6 +188,9 @@ sub _splice_slot { } } } + else { + $first_to_replace = $before->[-1]+1; + } splice @{$self->_storage}, $first_to_replace,$how_many, diff --git a/t/ranges-setting.t b/t/ranges-setting.t index b8d2a57..e8f4c77 100644 --- a/t/ranges-setting.t +++ b/t/ranges-setting.t @@ -24,6 +24,14 @@ sub test_it { }); } 'setting 30-50'; + lives_ok { + $obj->set({ + from => 25, + to => 27, + value => [7,8,9], + }); + } 'setting 30-50'; + cmp_deeply($obj->get({at => 15}), [1,2,3], 'getting 15'); @@ -50,6 +58,10 @@ sub test_it { $obj->get({at => 50}) } 'getting 50 dies'; + cmp_deeply($obj->get({at => 25}), + [7,8,9], + 'getting 25'); + dies_ok { $obj->get({at => 0}) } 'getting 0 dies'; |