| 1 | #======================================================================== | ||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||
| 3 | # Badger::Data | ||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 6 | # Base class module for objects representing various different data | ||||||||||||||||||||||||||||||||||||||
| 7 | # types. | ||||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||||
| 9 | # AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 10 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||
| 12 | #======================================================================== | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | package Badger::Data; | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | use Badger::Class | ||||||||||||||||||||||||||||||||||||||
| 17 | 1 | version => 0.01, | |||||||||||||||||||||||||||||||||||||
| 18 | debug => 0, | ||||||||||||||||||||||||||||||||||||||
| 19 | base => 'Badger::Prototype', | ||||||||||||||||||||||||||||||||||||||
| 20 | import => 'class CLASS', | ||||||||||||||||||||||||||||||||||||||
| 21 | utils => 'md5_hex self_params refaddr', | ||||||||||||||||||||||||||||||||||||||
| 22 | auto_can => 'method', # anything in $METHODS can become a method | ||||||||||||||||||||||||||||||||||||||
| 23 | constants => 'HASH TRUE FALSE', | ||||||||||||||||||||||||||||||||||||||
| 24 | constant => { | ||||||||||||||||||||||||||||||||||||||
| 25 | # tell Badger what class to strip off to generate id() | ||||||||||||||||||||||||||||||||||||||
| 26 | base_id => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||
| 27 | id => 'type', | ||||||||||||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||||||||||||
| 29 | alias => { | ||||||||||||||||||||||||||||||||||||||
| 30 | # alias type() to id() method provided by Badger::Base | ||||||||||||||||||||||||||||||||||||||
| 31 | type => 'id', | ||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | # Define a lexical scope with a $METADATA table for storing any out-of-band | ||||||||||||||||||||||||||||||||||||||
| 36 | # information about text objects. The methods defined below can be called | ||||||||||||||||||||||||||||||||||||||
| 37 | # as subroutines (as part of the vmethod mechanism), so the first argument | ||||||||||||||||||||||||||||||||||||||
| 38 | # can be a non-reference text string in place of the usual $self object | ||||||||||||||||||||||||||||||||||||||
| 39 | # reference (which itself is just a blessed reference to a scalar text | ||||||||||||||||||||||||||||||||||||||
| 40 | # string). To handle this case, we use an md5_hex encoding of the text | ||||||||||||||||||||||||||||||||||||||
| 41 | # to determine a unique handle for it (or close enough to unique for | ||||||||||||||||||||||||||||||||||||||
| 42 | # practical purposes) | ||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||||||||||||
| 45 | 1 | my $METADATA = { }; | |||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | sub metadata { | ||||||||||||||||||||||||||||||||||||||
| 48 | my $meta = $METADATA->{ | ||||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | $_[0] && ref $_[0] | ||||||||||||||||||||||||||||||||||||
|
Subroutine
metadata: called CC=7 SCAR=20.0
Pod
documented
Branch: 1 branch
Condition60%3/5
$$METADATA{$_[0] && CORE::ref $_[0] ? builtin::refaddr($_[0]) : md5_hex($_[0])} ||= {}
$_[0] and CORE::ref $_[0]
MC/DC100%1/1
$$METADATA{$_[0] && CORE::ref $_[0] ? builtin::refaddr($_[0]) : md5_hex($_[0])} ||= {}
$$METADATA{$_[0] && CORE::ref $_[0] ? builtin::refaddr($_[0]) : md5_hex($_[0])}
MC/DC0%0/2
$_[0] and CORE::ref $_[0]
$_[0]
CORE::ref $_[0]
Truth table100%2/2
A
A= $$METADATA{$_[0] && CORE::ref $_[0] ? builtin::refaddr($_[0]) : md5_hex($_[0])}
Truth table33%1/3
A and B
A= $_[0]B= CORE::ref $_[0]
| |||||||||||||||||||||||||||||||||||||||
| 50 | ? refaddr $_[0] | ||||||||||||||||||||||||||||||||||||||
| 51 | : md5_hex $_[0] | ||||||||||||||||||||||||||||||||||||||
| 52 | } ||= { }; | ||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | # short-cut: return metadata hash when called without arguments | ||||||||||||||||||||||||||||||||||||||
| 55 | 6 | ▶ | return $meta if @_ == 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | # short-cut: return item in the metadata hash when called with a | ||||||||||||||||||||||||||||||||||||||
| 58 | # single (non-hashref) item | ||||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | return $meta->{ $_[1] } if @_ == 2 && ref $_[1] ne HASH; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 2 and CORE::ref $_[1] ne "HASH"
MC/DC50%1/2
@_ == 2 and CORE::ref $_[1] ne "HASH"
@_ == 2
CORE::ref $_[1] ne "HASH"
Truth table66%2/3
A and B
A= @_ == 2B= CORE::ref $_[1] ne "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | # add metadata items when called with a HASH ref or multiple args | ||||||||||||||||||||||||||||||||||||||
| 62 | 1 | my ($self, $params) = self_params(@_); | |||||||||||||||||||||||||||||||||||||
| 63 | 1 | @$meta{ keys %$params } = values %$params; | |||||||||||||||||||||||||||||||||||||
| 64 | 1 | return $meta; | |||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | # This is a throwback to the Template::TT3::Type object on which this is | ||||||||||||||||||||||||||||||||||||||
| 69 | # based... these methods probably won't be staying here - they should be | ||||||||||||||||||||||||||||||||||||||
| 70 | # in Badger::Data::Type | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | 1 | our $METHODS = { | |||||||||||||||||||||||||||||||||||||
| 73 | method => \&method, # TODO: can() as alias to method()? | ||||||||||||||||||||||||||||||||||||||
| 74 | methods => \&methods, | ||||||||||||||||||||||||||||||||||||||
| 75 | type => \&type, | ||||||||||||||||||||||||||||||||||||||
| 76 | ref => \&ref, | ||||||||||||||||||||||||||||||||||||||
| 77 | def => \&defined, | ||||||||||||||||||||||||||||||||||||||
| 78 | undef => \&undefined, | ||||||||||||||||||||||||||||||||||||||
| 79 | defined => \&defined, | ||||||||||||||||||||||||||||||||||||||
| 80 | undefined => \&undefined, | ||||||||||||||||||||||||||||||||||||||
| 81 | true => \&true, | ||||||||||||||||||||||||||||||||||||||
| 82 | false => \&false, | ||||||||||||||||||||||||||||||||||||||
| 83 | }; | ||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | sub init { | ||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | my ($self, $config) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | # merge everything in $config into $self for now | ||||||||||||||||||||||||||||||||||||||
| 90 | 4 | @$self{ keys %$config } = values %$config; | |||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | # merge all config methods with class $METHODS | ||||||||||||||||||||||||||||||||||||||
| 93 | $self->{ methods } = $self->class->hash_vars( | ||||||||||||||||||||||||||||||||||||||
| 94 | METHODS => $config->{ methods } | ||||||||||||||||||||||||||||||||||||||
| 95 | 4 | ); | |||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | sub method { | ||||||||||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | my $self = shift->prototype; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | # return item from hash or the hash itself when called without arguments | ||||||||||||||||||||||||||||||||||||||
| 105 | return @_ | ||||||||||||||||||||||||||||||||||||||
| 106 | ? $self->{ methods }->{ $_[0] } | ||||||||||||||||||||||||||||||||||||||
| 107 | 2 | ▶ | : $self->{ methods }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | sub methods { | ||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | my $self = shift->prototype; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
methods: called CC=4 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||
| 114 | # return hash ref when called without argument | ||||||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | return $self->{ methods } unless @_; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | # add items to hash when called with hash ref or multiple args | ||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | my $items = @_ == 1 && ref $_[0] eq HASH ? shift : { @_ }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 and CORE::ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and CORE::ref $_[0] eq "HASH"
@_ == 1
CORE::ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= CORE::ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $hash = $self->{ methods }; | |||||||||||||||||||||||||||||||||||||
| 120 | 0 | @$hash{ keys %$items } = values %$items; | |||||||||||||||||||||||||||||||||||||
| 121 | 0 | return $hash; | |||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | sub ref { | ||||||||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | return CORE::ref($_[0]); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | sub defined { | ||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | CORE::defined $_[0] ? TRUE : FALSE; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
defined: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | sub undefined { | ||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | CORE::defined $_[0] ? FALSE : TRUE; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
undefined: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | sub true { | ||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $_[0] ? $_[0] : FALSE; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
true: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | sub false { | ||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $_[0] ? FALSE : TRUE; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
false: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||||||||||||