| 1 | package Hash::Storage; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 3 | our $VERSION = '0.03'; | |||||||||||||
| 4 | |||||||||||||||
| 5 | 3 | ▶ | use v5.10; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use Carp qw/croak/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use Class::Load qw/load_class/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 3 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||
| 12 | 3 | my $class = shift; | |||||||||||||
| 13 | 3 | my %args = @_; | |||||||||||||
| 14 | 3 | my $driver = $args{driver}; | |||||||||||||
| 15 | 3 | ▶ | croak "Wrong driver" unless ref $driver; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 3 | my $self = bless {}, $class; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 3 | ▶ | if ( ref $driver eq 'ARRAY' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 20 | 3 | my $driver_class = 'Hash::Storage::Driver::' . $driver->[0]; | |||||||||||||
| 21 | |||||||||||||||
| 22 | 3 | load_class($driver_class); | |||||||||||||
| 23 | |||||||||||||||
| 24 | 3 | ▶ | $self->{driver} = $driver_class->new( %{ $driver->[1] || {} } ); | ||||||||||||
|
Condition50%1/2
$driver->[1] || {}
MC/DC0%0/1
$driver->[1] || {}
$driver->[1]
Truth table50%1/2
A
A= $driver->[1]
| |||||||||||||||
| 25 | } elsif ( $driver->isa('Hash::Storage::Driver::Base') ) { | ||||||||||||||
| 26 | 0 | $self->{driver} = $driver; | |||||||||||||
| 27 | } else { | ||||||||||||||
| 28 | 0 | croak "Wrong driver [$driver]"; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 3 | $self->init(); | |||||||||||||
| 32 | |||||||||||||||
| 33 | 3 | return $self; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 3 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 37 | 3 | my $self = shift; | |||||||||||||
| 38 | 3 | $self->{driver}->init(@_); | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 21 | ▶ | sub get { | ||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 42 | 21 | my ( $self, $id ) = @_; | |||||||||||||
| 43 | 21 | ▶ | croak "id is required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 21 | ▶ | croak "id must contain only letters and digits" unless $self->_is_good_id($id); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | 21 | $self->{driver}->get(lc($id)); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 48 | ▶ | sub set { | ||||||||||||
|
Subroutine
set: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||
| 50 | 48 | my ( $self, $id, $fields ) = @_; | |||||||||||||
| 51 | 48 | ▶ | croak 'id is required' unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 48 | ▶ | croak 'id must contain only letters and digits' unless $self->_is_good_id($id); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 48 | ▶ | croak 'fields are required' unless ref $fields eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | |||||||||||||||
| 55 | 48 | $fields->{_id} = lc($id); | |||||||||||||
| 56 | 48 | $self->{driver}->set( lc($id), $fields ); | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 6 | ▶ | sub del { | ||||||||||||
|
Subroutine
del: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||
| 60 | 6 | my ( $self, $id ) = @_; | |||||||||||||
| 61 | 6 | ▶ | croak "id is required" unless $id; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 6 | ▶ | croak "id must contain only letters and digits" unless $self->_is_good_id($id); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | |||||||||||||||
| 64 | 6 | $self->{driver}->del(lc($id)); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 15 | ▶ | sub list { | ||||||||||||
|
Subroutine
list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 68 | 15 | my ( $self, @query ) = @_; | |||||||||||||
| 69 | 15 | $self->{driver}->list( @query ); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 15 | ▶ | sub count { | ||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 73 | 15 | my ( $self, $filter ) = @_; | |||||||||||||
| 74 | 15 | $self->{driver}->count($filter); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 75 | ▶ | sub _is_good_id { | ||||||||||||
|
Subroutine
_is_good_id: called CC=1 SCAR=0.0
| |||||||||||||||
| 78 | 75 | my ($self, $id) = @_; | |||||||||||||
| 79 | 75 | return 1; | |||||||||||||
| 80 | #return $id =~ m/^[a-zA-Z0-9][a-zA-Z0-9_\@\-.]*[a-zA-Z0-9]$/ ? 1 : 0 ; | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =head1 NAME | ||||||||||||||
| 84 | |||||||||||||||
| 85 | Hash::Storage - Persistent Hash Storage Framework | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =cut | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =head1 SYNOPSIS | ||||||||||||||
| 90 | |||||||||||||||
| 91 | my $st = Hash::Storage->new( | ||||||||||||||
| 92 | driver => [ OneFile => { serializer => 'JSON', file => '/tmp/t.json' } ] | ||||||||||||||
| 93 | ); | ||||||||||||||
| 94 | |||||||||||||||
| 95 | # Store hash by id | ||||||||||||||
| 96 | $st->set( 'user1' => { name => 'Viktor', gender => 'M', age => '28' } ); | ||||||||||||||
| 97 | |||||||||||||||
| 98 | # Get hash by id | ||||||||||||||
| 99 | my $user_data = $st->get('user1'); | ||||||||||||||
| 100 | |||||||||||||||
| 101 | # Delete hash by id | ||||||||||||||
| 102 | $st->del('user1'); | ||||||||||||||
| 103 | |||||||||||||||
| 104 | |||||||||||||||
| 105 | =head1 DESCRIPTION | ||||||||||||||
| 106 | |||||||||||||||
| 107 | Hash::Storage is a multipurpose storage for hash. You can consider Hash::Storage object as a collection of hashes. | ||||||||||||||
| 108 | You can use it for storing users, sessions and a lot more data. | ||||||||||||||
| 109 | |||||||||||||||
| 110 | Hash::Storage has pluggable architecture, therefore you can use different drivers or write you own. | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =head1 METHODS | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head2 Hash::Storage->new(driver => $DRIVER) | ||||||||||||||
| 115 | |||||||||||||||
| 116 | $DRIVER is an arrayref with two values: | ||||||||||||||
| 117 | the first is a driver name, the second is a hashref with options for driver. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | my $st = Hash::Storage->new( | ||||||||||||||
| 120 | driver => [ OneFile => { serializer => 'JSON', file => '/tmp/t.json' } ] | ||||||||||||||
| 121 | ); | ||||||||||||||
| 122 | |||||||||||||||
| 123 | $DRIVER - also can be a Hash::Storage driver object | ||||||||||||||
| 124 | |||||||||||||||
| 125 | my $drv = Hash::Storage::Driver::OneFile->new({ serializer => 'JSON', file => '/tmp/t.json' }); | ||||||||||||||
| 126 | my $st = Hash::Storage->new( driver => $drv ); | ||||||||||||||
| 127 | |||||||||||||||
| 128 | |||||||||||||||
| 129 | =head2 $SELF->set($ID, \%HASH); | ||||||||||||||
| 130 | |||||||||||||||
| 131 | Saves hash | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =head2 $SELF->get($ID); | ||||||||||||||
| 134 | |||||||||||||||
| 135 | Retrieves hash | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head2 $SELF->del($ID); | ||||||||||||||
| 138 | |||||||||||||||
| 139 | Deletes hash | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =head2 $SELF->list(); | ||||||||||||||
| 142 | |||||||||||||||
| 143 | returns array with hashrefs | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =head2 $SELF->count(); | ||||||||||||||
| 146 | |||||||||||||||
| 147 | returns number of hashes in a collection | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =head1 AUTHOR | ||||||||||||||
| 150 | |||||||||||||||
| 151 | "koorchik", C<< <"koorchik at cpan.org"> >> | ||||||||||||||
| 152 | |||||||||||||||
| 153 | =head1 BUGS | ||||||||||||||
| 154 | |||||||||||||||
| 155 | Please report any bugs or feature requests to C<bug-hash-storage at rt.cpan.org>, or through | ||||||||||||||
| 156 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Storage>. I will be notified, and then you'll | ||||||||||||||
| 157 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||
| 158 | |||||||||||||||
| 159 | =head1 SUPPORT | ||||||||||||||
| 160 | |||||||||||||||
| 161 | You can find documentation for this module with the perldoc command. | ||||||||||||||
| 162 | |||||||||||||||
| 163 | perldoc Hash::Storage | ||||||||||||||
| 164 | |||||||||||||||
| 165 | |||||||||||||||
| 166 | You can also look for information at: | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =over 4 | ||||||||||||||
| 169 | |||||||||||||||
| 170 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||
| 171 | |||||||||||||||
| 172 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-Storage> | ||||||||||||||
| 173 | |||||||||||||||
| 174 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 175 | |||||||||||||||
| 176 | L<http://annocpan.org/dist/Hash-Storage> | ||||||||||||||
| 177 | |||||||||||||||
| 178 | =item * CPAN Ratings | ||||||||||||||
| 179 | |||||||||||||||
| 180 | L<http://cpanratings.perl.org/d/Hash-Storage> | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =item * Search CPAN | ||||||||||||||
| 183 | |||||||||||||||
| 184 | L<http://search.cpan.org/dist/Hash-Storage/> | ||||||||||||||
| 185 | |||||||||||||||
| 186 | =back | ||||||||||||||
| 187 | |||||||||||||||
| 188 | |||||||||||||||
| 189 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 190 | |||||||||||||||
| 191 | |||||||||||||||
| 192 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 193 | |||||||||||||||
| 194 | Copyright 2012 "koorchik". | ||||||||||||||
| 195 | |||||||||||||||
| 196 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 197 | under the terms of either: the GNU General Public License as published | ||||||||||||||
| 198 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||
| 199 | |||||||||||||||
| 200 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||
| 201 | |||||||||||||||
| 202 | |||||||||||||||
| 203 | =cut | ||||||||||||||
| 204 | |||||||||||||||
| 205 | 3 | 1; # End of Hash::Storage | |||||||||||||