| 1 | package Daje::Tools::Datasections; | ||||||||||
| 2 | 2 | ▶ | use Mojo::Base -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use v5.42; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use Mojo::Loader qw {data_section load_class}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # Daje::Tools::Datasections - Load and store data sections in memory from a named *.pm | ||||||||||
| 8 | # | ||||||||||
| 9 | # | ||||||||||
| 10 | # Abstract | ||||||||||
| 11 | # ======== | ||||||||||
| 12 | # | ||||||||||
| 13 | # Get and store data sections from perl classes | ||||||||||
| 14 | # | ||||||||||
| 15 | # | ||||||||||
| 16 | # Synopsis | ||||||||||
| 17 | # ========= | ||||||||||
| 18 | # | ||||||||||
| 19 | # use Daje::Tools::Datasections; | ||||||||||
| 20 | # | ||||||||||
| 21 | # my $data = Daje::Tools::Datasections->new( | ||||||||||
| 22 | # | ||||||||||
| 23 | # data_sections => ['c1','c2','c3'], | ||||||||||
| 24 | # | ||||||||||
| 25 | # source => 'Class::Containing::Datasections | ||||||||||
| 26 | # | ||||||||||
| 27 | # )->load_data_sections(); | ||||||||||
| 28 | # | ||||||||||
| 29 | # METHODS | ||||||||||
| 30 | # ======= | ||||||||||
| 31 | # Get one section of loaded data | ||||||||||
| 32 | # | ||||||||||
| 33 | # my $c1 = $data->get_data_section('c1'); | ||||||||||
| 34 | # | ||||||||||
| 35 | # Add a section of data | ||||||||||
| 36 | # | ||||||||||
| 37 | # $data->set_data_section('new_section', 'section data'); | ||||||||||
| 38 | # | ||||||||||
| 39 | # Set a new source | ||||||||||
| 40 | # | ||||||||||
| 41 | # $data->set_source('New::Source'); | ||||||||||
| 42 | # | ||||||||||
| 43 | # Add a new section to load | ||||||||||
| 44 | # | ||||||||||
| 45 | # $data->add_data_section('test'); | ||||||||||
| 46 | # | ||||||||||
| 47 | # | ||||||||||
| 48 | # LICENSE | ||||||||||
| 49 | # ======= | ||||||||||
| 50 | # Generate::Tools::Datasections (the distribution) is licensed under the same terms as Perl. | ||||||||||
| 51 | # | ||||||||||
| 52 | # AUTHOR | ||||||||||
| 53 | # ====== | ||||||||||
| 54 | # Jan Eskilsson | ||||||||||
| 55 | # | ||||||||||
| 56 | # COPYRIGHT | ||||||||||
| 57 | # ========= | ||||||||||
| 58 | # Copyright (C) 2024 Jan Eskilsson. | ||||||||||
| 59 | # | ||||||||||
| 60 | |||||||||||
| 61 | 2 | our $VERSION = '1.00'; | |||||||||
| 62 | |||||||||||
| 63 | 2 | has 'data_sec'; | |||||||||
| 64 | 2 | has 'data_sections'; | |||||||||
| 65 | 2 | has 'source'; | |||||||||
| 66 | 2 | has 'error'; | |||||||||
| 67 | |||||||||||
| 68 | # Load all data_sections | ||||||||||
| 69 | 0 | ▶ | sub load_data_sections($self) { | ||||||||
|
Subroutine
load_data_sections: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | try { | |||||||||
| 72 | 0 | my $data_sec = {}; | |||||||||
| 73 | 0 | $self->data_sec($data_sec); | |||||||||
| 74 | 0 | my $class = $self->source(); | |||||||||
| 75 | 0 | my $c = load_class($class); | |||||||||
| 76 | 0 | my @data_sec = split(',', $self->data_sections) ; | |||||||||
| 77 | 0 | my $length = scalar @data_sec; | |||||||||
| 78 | 0 | ▶ | for(my $i = 0; $i < $length; $i++){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | my $section = $data_sec[$i]; | |||||||||
| 80 | 0 | ▶ | if (!exists($self->data_sec->{$section})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | my $sec = data_section($class, $section); | |||||||||
| 82 | 0 | $self->set_data_section($section, $sec); | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | } catch ($e) { | ||||||||||
| 86 | 0 | $self->error($e); | |||||||||
| 87 | }; | ||||||||||
| 88 | |||||||||||
| 89 | 0 | return 1; | |||||||||
| 90 | } | ||||||||||
| 91 | # Get one section | ||||||||||
| 92 | 0 | ▶ | sub get_data_section($self, $section) { | ||||||||
|
Subroutine
get_data_section: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 93 | 0 | return $self->data_sec->{$section}; | |||||||||
| 94 | } | ||||||||||
| 95 | # Set a section | ||||||||||
| 96 | 0 | ▶ | sub set_data_section($self, $key, $templ) { | ||||||||
|
Subroutine
set_data_section: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 97 | 0 | $self->data_sec->{$key} = $templ; | |||||||||
| 98 | } | ||||||||||
| 99 | # Set Source | ||||||||||
| 100 | 0 | ▶ | sub set_source($self, $source_class) { | ||||||||
|
Subroutine
set_source: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 101 | 0 | $self->source = $source_class; | |||||||||
| 102 | } | ||||||||||
| 103 | # Add a section to the data sections_array to be loaded | ||||||||||
| 104 | 0 | ▶ | sub add_data_section($self, $section) { | ||||||||
|
Subroutine
add_data_section: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 105 | 0 | $self->data_sections .','. $section; | |||||||||
| 106 | } | ||||||||||
| 107 | 2 | 1; | |||||||||
| 108 | |||||||||||
| 109 | __END__ | ||||||||||