File Coverage

blib/lib/Data/Record/Serialize/Util.pm
Criterion Covered Total %
statement 32 32 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 0 2 0.0
total 42 44 95.4


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Util;
2              
3             # ABSTRACT: Useful things
4              
5 17     17   226 use v5.12;
  17         64  
6 17     17   105 use strict;
  17         47  
  17         416  
7 17     17   90 use warnings;
  17         42  
  17         847  
8             our $VERSION = '1.05';
9              
10 17     17   125 use parent 'Exporter::Tiny';
  17         66  
  17         135  
11              
12             my @TYPE_CATEGORY_NAMES;
13             my %TYPES;
14             BEGIN {
15 17     17   2067 @TYPE_CATEGORY_NAMES = qw(
16             ANY
17             INTEGER
18             FLOAT
19             NUMBER
20             STRING
21             NOT_STRING
22             BOOLEAN
23             );
24              
25 17         634 %TYPES = (
26             T_INTEGER => 'I',
27             T_NUMBER => 'N',
28             T_STRING => 'S',
29             T_BOOLEAN => 'B',
30             );
31             }
32              
33 17     17   9242 use enum @TYPE_CATEGORY_NAMES;
  17         20967  
  17         152  
34 17     17   9821 use constant \%TYPES;
  17         75  
  17         2053  
35              
36             our @TYPE_CATEGORIES = map {
37             ; # add a ; to help 5.10
38 17     17   116 no strict 'refs'; ## no critic(ProhibitNoStrict)
  17         49  
  17         7738  
39             $_->();
40             } @TYPE_CATEGORY_NAMES;
41              
42             our %EXPORT_TAGS = (
43             types => [ keys %TYPES ],
44             categories => \@TYPE_CATEGORY_NAMES,
45             subs => [qw( is_type index_types )],
46             );
47              
48             our @EXPORT_OK = map { @{$_} } values %EXPORT_TAGS;
49              
50             my @TypeRE;
51             $TypeRE[ $_->[0] ] = $_->[1]
52             for [ +( ANY ) => qr/.*/ ],
53             [ +( STRING ) => qr/^S/i ],
54             [ +( FLOAT ) => qr/^N/i ],
55             [ +( INTEGER ) => qr/^I/i ],
56             [ +( BOOLEAN ) => qr/^B/i ],
57             [ +( NUMBER ) => qr/^[NI]/i ],
58             [ +( NOT_STRING ) => qr/^[^S]+/ ];
59              
60             sub is_type {
61 92     92 0 178 my ( $type, $type_enum ) = @_;
62 92         499 $type =~ $TypeRE[$type_enum];
63             }
64              
65             sub index_types {
66 56     56 0 196 my ( $types ) = @_;
67              
68 56         219 my @fields = keys %$types;
69 56         125 my @type_index;
70              
71 56         176 for my $category ( @TYPE_CATEGORIES ) {
72 392         710 my $re = $TypeRE[$category];
73 392         620 $type_index[$category] = [ grep { $types->{$_} =~ $re } @fields ];
  945         3864  
74             }
75              
76 56         385 return \@type_index;
77             }
78              
79             1;
80              
81             #
82             # This file is part of Data-Record-Serialize
83             #
84             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
85             #
86             # This is free software, licensed under:
87             #
88             # The GNU General Public License, Version 3, June 2007
89             #
90              
91             __END__
92              
93             =pod
94              
95             =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory
96              
97             =head1 NAME
98              
99             Data::Record::Serialize::Util - Useful things
100              
101             =head1 VERSION
102              
103             version 1.05
104              
105             =head1 INTERNALS
106              
107             =for Pod::Coverage index_types
108             is_type
109              
110             =head1 SUPPORT
111              
112             =head2 Bugs
113              
114             Please report any bugs or feature requests to bug-data-record-serialize@rt.cpan.org or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Record-Serialize>
115              
116             =head2 Source
117              
118             Source is available at
119              
120             https://gitlab.com/djerius/data-record-serialize
121              
122             and may be cloned from
123              
124             https://gitlab.com/djerius/data-record-serialize.git
125              
126             =head1 SEE ALSO
127              
128             Please see those modules/websites for more information related to this module.
129              
130             =over 4
131              
132             =item *
133              
134             L<Data::Record::Serialize|Data::Record::Serialize>
135              
136             =back
137              
138             =head1 AUTHOR
139              
140             Diab Jerius <djerius@cpan.org>
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
145              
146             This is free software, licensed under:
147              
148             The GNU General Public License, Version 3, June 2007
149              
150             =cut