File Coverage

blib/lib/Lab/Moose/DataFile/Meta.pm
Criterion Covered Total %
statement 47 51 92.1
branch 6 10 60.0
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 65 73 89.0


line stmt bran cond sub pod time code
1             package Lab::Moose::DataFile::Meta;
2             #ABSTRACT: YAML Metadata file
3             $Lab::Moose::DataFile::Meta::VERSION = '3.881';
4 27     27   372 use v5.20;
  27         102  
5              
6 27     27   170 use warnings;
  27         60  
  27         702  
7 27     27   143 use strict;
  27         70  
  27         488  
8              
9 27     27   128 use Moose;
  27         67  
  27         187  
10 27     27   176447 use MooseX::Params::Validate;
  27         105  
  27         200  
11              
12 27     27   12126 use Carp;
  27         90  
  27         1665  
13              
14 27     27   200 use Time::HiRes qw/gettimeofday tv_interval/;
  27         80  
  27         189  
15 27     27   2900 use YAML::XS;
  27         76  
  27         1679  
16 27     27   187 use Fcntl 'SEEK_SET';
  27         84  
  27         1703  
17              
18 27     27   193 use namespace::autoclean;
  27         55  
  27         193  
19              
20              
21             extends 'Lab::Moose::DataFile';
22              
23             has mode => (
24             is => 'ro',
25             default => '>>',
26             init_arg => undef
27             );
28              
29             has meta_data => (
30             is => 'ro',
31             isa => 'ArrayRef | HashRef',
32             writer => '_meta_data',
33             init_arg => undef
34             );
35              
36             has start_time => (
37             is => 'ro',
38             isa => 'ArrayRef[Int]',
39             init_arg => undef,
40             writer => '_start_time'
41             );
42              
43             sub log {
44 75     75 1 2345 my $self = shift;
45 75         393 my ($meta)
46             = validated_list( \@_, meta => { isa => 'ArrayRef | HashRef' } );
47              
48 75         25809 my $existing_meta = $self->meta_data();
49 75 100       224 if ( defined $existing_meta ) {
50 2         6 my $meta_type = ref $meta;
51 2         8 my $existing_meta_type = ref $existing_meta;
52              
53 2 50       10 if ( $meta_type ne $existing_meta_type ) {
54 0         0 croak "meta has reftype $meta_type "
55             . "while existing meta has reftype $existing_meta_type";
56             }
57              
58 2 50       8 if ( $meta_type eq 'ARRAY' ) {
59 0         0 $existing_meta = [ @{$existing_meta}, @{$meta} ];
  0         0  
  0         0  
60             }
61             else {
62 2         4 $existing_meta = { %{$existing_meta}, %{$meta} };
  2         8  
  2         15  
63             }
64              
65 2         85 $self->_meta_data($existing_meta);
66             }
67             else {
68 73         2384 $self->_meta_data($meta);
69             }
70              
71 75         2113 my $fh = $self->filehandle();
72              
73 75 50       2224 truncate $fh, 0
74             or croak "truncate failed: $!";
75 75 50       798 seek $fh, 0, SEEK_SET
76             or croak "seek failed: $!";
77              
78 75         186 print {$fh} Dump( $self->meta_data );
  75         2873  
79             }
80              
81             __PACKAGE__->meta->make_immutable();
82              
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             Lab::Moose::DataFile::Meta - YAML Metadata file
95              
96             =head1 VERSION
97              
98             version 3.881
99              
100             =head1 SYNOPSIS
101              
102             my $file = Lab::Moose::DataFile::Meta->new(
103             folder => $folder,
104             filename => 'metafile.yml'
105             );
106              
107             $file->log(meta => {key1 => $value1, key2 => $value2});
108              
109             =head1 METHODS
110              
111             =head2 log
112              
113             Log either an hashref or an arrayref to the log file.
114             Augment the file's contents. The new keys/items will be merged with the
115             existing ones. Rewrites the file with the new contents.
116             You may not mixup calls with hashref and arrayref arguments.
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
121              
122             Copyright 2016 Simon Reinhardt
123             2017 Andreas K. Huettel
124             2020 Andreas K. Huettel
125              
126              
127             This is free software; you can redistribute it and/or modify it under
128             the same terms as the Perl 5 programming language system itself.
129              
130             =cut