Hopen/G/Entity.pm

filter: Statementstmt 100% 31 / 31 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 98.1% 52 / 53 CC 6 SCAR 9.0
CC
6
Cov
98%
name
5.0
_stringify
2.0
BEGIN
1.0
CRAP 2.5
« Hopen/OrderedPredecessorGraph.pm ↑ Summary Hopen/Scope/Environment.pm »
1 # Data::Hopen::G::Entity - base class for hopen's data model
2 package Data::Hopen::G::Entity;
3 16 ▶use Data::Hopen;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 our $VERSION = '0.000018';
11  
12 sub name;
13  
14 16 ▶use Class::Tiny qw(name);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Data::Hopen::G::Entity - The base class for all hopen nodes and edges
19  
20 =head1 SYNOPSIS
21  
22 hopen creates and manages a graph of entities: nodes and edges. This class
23 holds common information.
24  
25 =head1 MEMBERS
26  
27 =head2 name
28  
29 The name of this entity. The name is for human consumption and is not used by
30 hopen to make any decisions. However, node names starting with an underscore
31 are reserved for hopen's internal use.
32  
33 The name C<'0'> (a single digit zero) is forbidden (since it's falsy).
34  
35 =cut
36  
37 =head1 FUNCTIONS
38  
39 =head2 name
40  
41 A custom accessor for name. If no name has been stored, return the stringifed
42 version of the entity. That way every entity always has a name.
43  
44 =cut
45  
46 sub name {
47 422 ▶ croak 'Need an instance' unless ref $_[0];
Subroutine name: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 1 421 422
48 # Note: avoiding `shift` since I've had problems with that in the past
49 # in classes that overload stringification.
50  
51 421 ▶ if (@_>1) { # Setter
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 8 413 421
elsif ($_[0]{'name'}) { } 375 38 413
52 8 ▶ croak "Name `$_[1]' is disallowed" unless !!$_[1]; # no falsy names
Branch: 1 branch
BranchTrueFalseTotal
if not $_[1] 0 8 8
53 8 return $_[0]->{name} = $_[1];
54 } elsif ( $_[0]->{name} ) { # Getter
55 375 return $_[0]->{name};
56 } else { # Default
57 38 return overload::StrVal($_[0]);
58 }
59 } #name()
60  
61 =head2 has_custom_name
62  
63 Returns truthy if a name has been set using L</name>.
64  
65 =cut
66  
67 2590 ▶sub has_custom_name { !!($_[0]->{name}) }
Subroutine has_custom_name: called CC=1 SCAR=0.0
Pod documented
68  
69 =head2 Stringification
70  
71 Stringifies to the name plus, if the name is custom, the refaddr.
72  
73 =cut
74  
75 sub _stringify {
76 $_[0]->has_custom_name ?
77 2574 ▶ sprintf("%s (%x)", $_[0]->{name}, refaddr $_[0]) :
Subroutine _stringify: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->has_custom_name ? : 2514 60 2574
78 overload::StrVal($_[0]);
79 } #_stringify
80  
81 16 use overload fallback => 1,
82 16 ▶ '""' => \&_stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 16 1;
85 __END__
« Hopen/OrderedPredecessorGraph.pm ↑ Summary Hopen/Scope/Environment.pm »