| 1 | 26 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 26 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 26 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package Class::Scaffold::Storable; | ||||||||||
| 6 | 26 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 26 | $Class::Scaffold::Storable::VERSION = '1.102280'; | |||||||||
| 8 | } | ||||||||||
| 9 | |||||||||||
| 10 | # ABSTRACT: Base class for all framework classes that support a storage. | ||||||||||
| 11 | 26 | ▶ | use parent 'Class::Scaffold::Base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 26 | __PACKAGE__->mk_scalar_accessors(qw(storage_type)) | |||||||||
| 13 | ->mk_hash_accessors(qw(storage_info)); | ||||||||||
| 14 | |||||||||||
| 15 | # Don't store the storage object itself, store the method we need to call on | ||||||||||
| 16 | # the delegate to get the storage object. This is just a little overhead, but | ||||||||||
| 17 | # saves us from a lot of headache when serializing and deserializing objects | ||||||||||
| 18 | # with Storable's freeze() and thaw(), because storage objects can't be | ||||||||||
| 19 | # deserialized properly. | ||||||||||
| 20 | # | ||||||||||
| 21 | # Impose a certain order on how the constructor args are processed. We want | ||||||||||
| 22 | # the storage to be set first, because other properties could be defined using | ||||||||||
| 23 | # mk_framework_object_accessors(). Now if the args were set in an arbitrary | ||||||||||
| 24 | # order, the framework_object-properties could be processed before the storage | ||||||||||
| 25 | # is set, which would cause an error, because the storage wouldn't be set yet, | ||||||||||
| 26 | # so it can't be asked to make an object. | ||||||||||
| 27 | # | ||||||||||
| 28 | # We can't have storage_type as a key within the storage_info hash, because we | ||||||||||
| 29 | # want to be able to set it directly if passed as an argument to the | ||||||||||
| 30 | # constructor; we also need to be able to prefer it in | ||||||||||
| 31 | # Class::Scaffold::Storable::FIRST_CONSTRUCTOR_ARGS(). | ||||||||||
| 32 | # | ||||||||||
| 33 | # We use the storage's signature as the id key, i.e. to find the id of the | ||||||||||
| 34 | # object within the storage. It would not be sufficient to use the storage's | ||||||||||
| 35 | # package name as the hash key because we can think of a multiplex storage | ||||||||||
| 36 | # that multiplexes onto two file system paths. In that case each of the | ||||||||||
| 37 | # multiplexed storages would have the same package name. And we can't use the | ||||||||||
| 38 | # storage's memory address (0x012345678) because different stages can be run | ||||||||||
| 39 | # within different processes and on different machines. | ||||||||||
| 40 | # | ||||||||||
| 41 | # For example, the attributes of an object of this class might look like: | ||||||||||
| 42 | # storage_type: core_storage | ||||||||||
| 43 | # storage_info: | ||||||||||
| 44 | # id: | ||||||||||
| 45 | # 'Registry::NICAT::Storage::DBI::Oracle::NICAT,dbname=db.test,dbuser=nic': id12345 | ||||||||||
| 46 | # 'Some::File::Storage,fspath=/path/to/storage/root': id45678 | ||||||||||
| 47 | # This example assumes that the core storage is multiplexing on a DBI storage | ||||||||||
| 48 | # and a file system storage. | ||||||||||
| 49 | 26 | ▶ | use constant FIRST_CONSTRUCTOR_ARGS => ('storage_type'); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 26 | ▶ | use constant SKIP_COMPARABLE_KEYS => (qw/storage_type storage_info/); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 26 | ▶ | use constant HYGIENIC => (qw/storage storage_type/); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub MUNGE_CONSTRUCTOR_ARGS { | ||||||||
|
Subroutine
MUNGE_CONSTRUCTOR_ARGS: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 54 | 0 | my ($self, @args) = @_; | |||||||||
| 55 | |||||||||||
| 56 | # needed in order to mix object creation of a given class with and without | ||||||||||
| 57 | # explicitly setting the storage object for it (Erik P. Ostlyngen, NORID): | ||||||||||
| 58 | 0 | ▶ | if (@args % 2 == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | my %args = @args; | |||||||||
| 60 | 0 | ▶ | return %args if $args{storage_type}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # The superclass does nothing, so we'll skip this for performance reasons | ||||||||||
| 64 | # - this method is called very often. | ||||||||||
| 65 | # @args = $self->SUPER::MUNGE_CONSTRUCTOR_ARGS(@args); | ||||||||||
| 66 | 0 | our %cache; | |||||||||
| 67 | 0 | my $extra_args; | |||||||||
| 68 | 0 | ▶ | unless ($extra_args = $cache{ ref $self }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | my $object_type = $self->get_my_factory_type; | |||||||||
| 70 | 0 | ▶ | if (defined $object_type) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | my $storage_type = | |||||||||
| 72 | $self->delegate->get_storage_type_for($object_type); | ||||||||||
| 73 | 0 | $self->delegate->$storage_type->lazy_connect; | |||||||||
| 74 | |||||||||||
| 75 | # storage will be disconnected in Class::Scaffold::App->app_finish | ||||||||||
| 76 | 0 | $extra_args = $cache{ ref $self } = | |||||||||
| 77 | [ storage_type => $storage_type ]; | ||||||||||
| 78 | } else { | ||||||||||
| 79 | 0 | $extra_args = $cache{ ref $self } = []; | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | 0 | (@args, @$extra_args); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub storage { | ||||||||
|
Subroutine
storage: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 86 | 0 | my $self = shift; | |||||||||
| 87 | 0 | my $method = $self->storage_type; | |||||||||
| 88 | 0 | ▶ | if ($method) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $self->delegate->$method; | |||||||||
| 90 | } else { | ||||||||||
| 91 | 0 | local $Error::Depth = $Error::Depth + 1; | |||||||||
| 92 | 0 | throw Error::Hierarchy::Internal::CustomMessage(custom_message => | |||||||||
| 93 | "can't find method to get storage object from delegate"); | ||||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub id { | ||||||||
|
Subroutine
id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 98 | 0 | my $self = shift; | |||||||||
| 99 | 0 | my $storage = shift; | |||||||||
| 100 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | my $id = shift; | |||||||||
| 102 | 0 | $self->storage_info->{id}{ $storage->signature } = $id; | |||||||||
| 103 | } else { | ||||||||||
| 104 | 0 | $self->storage_info->{id}{ $storage->signature }; | |||||||||
| 105 | } | ||||||||||
| 106 | } | ||||||||||
| 107 | 26 | 1; | |||||||||
| 108 | |||||||||||
| 109 | |||||||||||
| 110 | __END__ | ||||||||||