App/Chart/Sympred.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/Chart/Suffix/NZ.pm ↑ Summary App/Chart/LatestHandler.pm »
1 # Symbol predicates.
2  
3 # Copyright 2007, 2008, 2009, 2010, 2013, 2015, 2016 Kevin Ryde
4  
5 # This file is part of Chart.
6 #
7 # Chart is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3, or (at your option) any later version.
10 #
11 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Chart. If not, see <http://www.gnu.org/licenses/>.
18  
19 package App::Chart::Sympred;
20 1 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶use App::Chart;
Subroutine BEGIN: called CC=1 SCAR=2.6
28  
29 sub validate {
30 my ($obj) = @_;
31 if (! (Scalar::Util::blessed ($obj) && $obj->isa (__PACKAGE__))) {
32 croak 'Not a symbol predicate: ' . ($obj||'undef');
33 }
34 }
35  
36  
37 #------------------------------------------------------------------------------
38  
39 package App::Chart::Sympred::Equal;
40 use strict;
41 use warnings;
42 use base 'App::Chart::Sympred';
43  
44 sub new {
45 my ($class, $suffix) = @_;
46 return bless { suffix => $suffix }, $class;
47 }
48 sub match {
49 my ($self, $symbol) = @_;
50 return ($symbol eq $self->{'suffix'});
51 }
52  
53 #------------------------------------------------------------------------------
54  
55 package App::Chart::Sympred::Suffix;
56 use 5.006;
57 use strict;
58 use warnings;
59 use base 'App::Chart::Sympred';
60  
61 sub new {
62 my ($class, $suffix) = @_;
63 if ($suffix =~ /\..*\./) {
64 # two or more dots
65 return App::Chart::Sympred::Regexp->new (qr/\Q$suffix\E$/);
66 } else {
67 return bless { suffix => $suffix }, $class;
68 }
69 }
70 sub match {
71 my ($self, $symbol) = @_;
72 return (App::Chart::symbol_suffix ($symbol) eq $self->{'suffix'});
73 }
74  
75 #------------------------------------------------------------------------------
76  
77 package App::Chart::Sympred::Prefix;
78 use strict;
79 use warnings;
80 use base 'App::Chart::Sympred';
81  
82 sub new {
83 my ($class, $prefix) = @_;
84 return bless { prefix => $prefix }, $class;
85 }
86  
87 sub match {
88 my ($self, $symbol) = @_;
89 return ($symbol =~ /^\Q$self->{'prefix'}\E/);
90 }
91  
92 #------------------------------------------------------------------------------
93  
94 package App::Chart::Sympred::Regexp;
95 use strict;
96 use warnings;
97 use base 'App::Chart::Sympred';
98  
99 sub new {
100 my ($class, $pattern) = @_;
101 return bless { pattern => $pattern }, $class;
102 }
103  
104 sub match {
105 my ($self, $symbol) = @_;
106 return ($symbol =~ m/$self->{'pattern'}/);
107 }
108  
109 #------------------------------------------------------------------------------
110  
111 package App::Chart::Sympred::Proc;
112 use strict;
113 use warnings;
114 use base 'App::Chart::Sympred';
115  
116 sub new {
117 my ($class, $proc) = @_;
118 return bless { proc => $proc }, $class;
119 }
120  
121 sub match {
122 my ($self, $symbol) = @_;
123 return &{$self->{'proc'}} ($symbol);
124 }
125  
126 #------------------------------------------------------------------------------
127  
128 package App::Chart::Sympred::Any;
129 use strict;
130 use warnings;
131 use base 'App::Chart::Sympred';
132  
133 sub new {
134 my ($class, @preds) = @_;
135 foreach my $pred (@preds) { App::Chart::Sympred::validate ($pred); }
136 return bless { preds => \@preds }, $class;
137 }
138  
139 sub add {
140 my ($self, @newpreds) = @_;
141 foreach my $pred (@newpreds) { App::Chart::Sympred::validate ($pred); }
142 push @{$self->{'preds'}}, @newpreds;
143 }
144  
145 sub match {
146 my ($self, $symbol) = @_;
147 return List::Util::first { $_->match($symbol) } @{$self->{'preds'}};
148 }
149  
150 1;
151 __END__
« App/Chart/Suffix/NZ.pm ↑ Summary App/Chart/LatestHandler.pm »