blib/lib/CXC/Gnuplot/V1/Bound.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« blib/lib/CXC/Gnuplot/V1/Base.pm ↑ Summary blib/lib/CXC/Gnuplot/V1/Bound/Numeric.pm »
1 package CXC::Gnuplot::V1::Bound;
2  
3 1 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Object::Pad 0.821;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use experimental 'builtin';
6  
7 our $VERSION = 'v0.29.3';
8  
9 class CXC::Gnuplot::V1::Bound
10 : lexical_new
11 : isa(CXC::Gnuplot::V1::Base) does( CXC::Gnuplot::V1::Role::Clone );
12  
13 use Lexical::Import qw( Ref::Util is_plain_hashref is_ref );
14 use Lexical::Import qw( List::Util reduce);
15  
16 use CXC::Gnuplot::V1::Types -lexical => qw(is_Num);
17 use CXC::Gnuplot::V1::Util -lexical => 'to_hash_r';
18  
19 use Text::Balanced;
20 use Regexp::Common 'number', 'delimited';
21 use builtin 'true', 'load_module';
22  
23 my $RE_quoted;
24 my $RE_real;
25 BEGIN {
26 $RE_real = ${RE}{num}{real};
27 use feature 'multidimensional';
28 $RE_quoted = ${RE}{quoted}{ -delim => q{'"} }{-esc}; ## no critic (Multidimensional)
29 }
30  
31 ## no critic(RegularExpressions::ProhibitComplexRegexes)
32 use constant RE_BOUND_REAL => qr/^$RE_real$/;
33 use constant RE_BOUND_QUOTED => qr/^$RE_quoted$/;
34  
35 use constant RE_BOUND => qr{
36 ^
37 (?:
38 (?<lower_bound> (?<lb_real>$RE_real) | (?<lb_q>$RE_quoted))
39 \s* <
40 )?
41 \s* (?<bound>[*]) \s*
42 (?: <
43 \s*
44 (?<upper_bound> (?<ub_real>$RE_real) | (?<ub_q>$RE_quoted))
45 )?
46 $ }x;
47  
48 use constant Numeric => 'CXC::Gnuplot::V1::Bound::Numeric';
49 use constant TimeDate => 'CXC::Gnuplot::V1::Bound::TimeDate';
50  
51 use overload q{""} => 'to_string', bool => sub { true }, fallback => true;
52  
53 field $lower_bound : param : reader = undef;
54 field $bound : param : reader = q{*};
55 field $upper_bound : param : reader = undef;
56  
57 my sub croak {
58 require Carp;
59 goto \&Carp::croak;
60 }
61  
62 my sub is_delimited ( $str ) {
63 return defined( ( Text::Balanced::extract_delimited( $str ) )[0] );
64 }
65  
66 my sub load_subclasses {
67 load_module( Numeric );
68 load_module( TimeDate );
69 1;
70 }
71  
72 my sub parse_from_bound ( $bound ) {
73  
74 state $foo = load_subclasses; ## no critic (UnusedVar)
75  
76 croak( '"bound" argument is not a scalar' )
77 if is_ref( $bound );
78  
79 return Numeric->new( bound => $bound )
80 if $bound =~ RE_BOUND_REAL;
81  
82 return TimeDate->new( bound => $bound )
83 if $bound =~ RE_BOUND_QUOTED;
84  
85 croak "illegal bounds specification: $bound"
86 unless $bound =~ RE_BOUND;
87  
88 my %args = %+;
89  
90 my $is_number = reduce { $a || $b } map { defined delete( $args{$_} ) } 'lb_real', 'ub_real';
91 my $is_quoted = reduce { $a || $b } map { defined delete( $args{$_} ) } 'lb_q', 'ub_q';
92  
93 croak q{illegal bounds specification: can't mix numeric and timedate}
94 if $is_number && $is_quoted;
95  
96 return Numeric->new( %args )
97 if $is_number;
98  
99 return TimeDate->new( %args )
100 if $is_quoted;
101  
102 return Numeric->new( %args );
103 }
104  
105 ADJUST {
106 if ( defined $bound && $bound =~ /</ ) {
107 my $parsed = parse_from_bound( $bound );
108  
109 $lower_bound = $parsed->lower_bound;
110 $bound = $parsed->bound;
111 $upper_bound = $parsed->upper_bound;
112 }
113 }
114  
115 method BUILDARGS : common ( @args ) {
116 @args == 1 && !is_plain_hashref( $args[0] )
117 and return ( bound => $args[0] );
118  
119 return $class->SUPER::BUILDARGS( @args );
120 }
121  
122 method new : common ( @args ) {
123  
124 return &new( $class, @args ) ## no critic (Ampersand )
125 # Object::Pad 0.825 segv's if __CLASS__ is used with a class
126 # method so use __PACKAGE__ instead, which is fine here.
127 if $class ne __PACKAGE__;
128  
129 my %arg = $class->BUILDARGS( @args );
130  
131 return parse_from_bound( $arg{bound} )
132 if defined $arg{bound}
133 && !defined $arg{lower_bound}
134 && !defined $arg{upper_bound};
135  
136 state $foo = load_subclasses; ## no critic (UnusedVar)
137  
138 if ( ( !defined $arg{lower_bound} || is_Num( $arg{lower_bound} ) )
139 && ( !defined $arg{upper_bound} || is_Num( $arg{upper_bound} ) )
140 && ( !defined $arg{bound} || is_Num( $arg{bound} ) || $arg{bound} eq q{*} ) )
141 {
142 return Numeric->new( %arg );
143 }
144  
145 if ( ( !defined $arg{lower_bound} || is_delimited( $arg{lower_bound} ) )
146 && ( !defined $arg{upper_bound} || is_delimited( $arg{upper_bound} ) )
147 && ( !defined $arg{bound} || is_delimited( $arg{bound} ) || $arg{bound} eq q{*} ) )
148 {
149 return TimeDate->new( %arg );
150 }
151  
152 croak( 'unable to construct object from arguments' );
153  
154 # what about geographic??
155 }
156  
157 method to_hash {
158 return to_hash_r( {
159 ( defined $lower_bound ? ( lower_bound => $lower_bound ) : () ),
160 bound => $bound,
161 ( defined $upper_bound ? ( upper_bound => $upper_bound ) : () ),
162 } );
163 }
164  
165 method to_string {
166 return join q{ < }, grep { defined } $lower_bound, $bound, $upper_bound;
167 }
168  
169  
170 1;
171  
172 #
173 # This file is part of CXC-Gnuplot
174 #
175 # This software is Copyright (c) 2024 by Smithsonian Astrophysical Observatory.
176 #
177 # This is free software, licensed under:
178 #
179 # The GNU General Public License, Version 3, June 2007
180 #
181  
182 __END__
« blib/lib/CXC/Gnuplot/V1/Base.pm ↑ Summary blib/lib/CXC/Gnuplot/V1/Bound/Numeric.pm »