| 1 | #======================================================================== | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # Badger::Codecs | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # DESCRIPTION | ||||||||||||
| 6 | # Manager of Badger::Codec modules for encoding and decoding data. | ||||||||||||
| 7 | # | ||||||||||||
| 8 | # AUTHOR | ||||||||||||
| 9 | # Andy Wardley <abw@wardley.org> | ||||||||||||
| 10 | # | ||||||||||||
| 11 | #======================================================================== | ||||||||||||
| 12 | |||||||||||||
| 13 | package Badger::Codecs; | ||||||||||||
| 14 | |||||||||||||
| 15 | 15 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 15 | ▶ | use Badger::Codec::Chain 'CHAIN CHAINED'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | use Badger::Factory::Class | ||||||||||||
| 18 | 15 | version => 0.01, | |||||||||||
| 19 | debug => 0, | ||||||||||||
| 20 | item => 'codec', | ||||||||||||
| 21 | path => 'Badger(X)::Codec', | ||||||||||||
| 22 | import => 'class CLASS', | ||||||||||||
| 23 | constants => 'HASH ARRAY DELIMITER PKG', | ||||||||||||
| 24 | constant => { | ||||||||||||
| 25 | CODEC_METHOD => 'codec', | ||||||||||||
| 26 | ENCODE_METHOD => 'encode', | ||||||||||||
| 27 | DECODE_METHOD => 'decode', | ||||||||||||
| 28 | ENCODING => 'Badger::Codec::Encoding', | ||||||||||||
| 29 | }, | ||||||||||||
| 30 | exports => { | ||||||||||||
| 31 | any => 'Codec', | ||||||||||||
| 32 | 15 | ▶ | }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | |||||||||||||
| 34 | our $CODECS = { | ||||||||||||
| 35 | # any codecs with non-standard capitalisation can go here, but | ||||||||||||
| 36 | # generally we grok the module name from the $CODEC_PATH, e.g. | ||||||||||||
| 37 | uri => 'Badger::Codec::URI', | ||||||||||||
| 38 | url => 'Badger::Codec::URL', | ||||||||||||
| 39 | yaml => 'Badger::Codec::YAML', | ||||||||||||
| 40 | json => 'Badger::Codec::JSON', | ||||||||||||
| 41 | html => 'Badger::Codec::HTML', | ||||||||||||
| 42 | tt => 'Badger::Codec::TT', | ||||||||||||
| 43 | map { | ||||||||||||
| 44 | 15 | my $name = $_; $name =~ s/\W//g; | |||||||||||
| 45 | 90 | $_ => [ENCODING, ENCODING.PKG.$name], | |||||||||||
| 46 | } qw( utf8 UTF8 UTF16BE UTF16LE UTF32BE UTF32LE ) | ||||||||||||
| 47 | }; | ||||||||||||
| 48 | |||||||||||||
| 49 | |||||||||||||
| 50 | sub Codec { | ||||||||||||
| 51 | 0 | ▶ | CLASS->codec(@_); | ||||||||||
|
Subroutine
Codec: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | |||||||||||||
| 55 | sub codec { | ||||||||||||
| 56 | 54 | ▶ | my $self = shift->prototype; | ||||||||||
|
Subroutine
codec: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 57 | |||||||||||||
| 58 | # quick turn-around if we're handling chains | ||||||||||||
| 59 | 54 | ▶ | return $_[0] =~ CHAINED | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | ? $self->chain(@_) | ||||||||||||
| 61 | : $self->item(@_); | ||||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | |||||||||||||
| 65 | sub chain { | ||||||||||||
| 66 | 4 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
chain: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 67 | 4 | ▶ | $self->debug("creating chain for $_[0]\n") if $DEBUG; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 4 | return CHAIN->new(@_); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | |||||||||||||
| 72 | sub found_object { | ||||||||||||
| 73 | 25 | ▶ | my ($self, $name, $item, $args) = @_; | ||||||||||
|
Subroutine
found_object: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 74 | |||||||||||||
| 75 | # TODO: assert $item is a codec object? | ||||||||||||
| 76 | |||||||||||||
| 77 | # We cache any codecs that are created without configuration items | ||||||||||||
| 78 | # but we can only use those "bare" codecs if $args is empty. Otherwise | ||||||||||||
| 79 | # we must create a new object | ||||||||||||
| 80 | 25 | ▶ | if (@$args) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 1 | $self->debug("creating new ", ref $item, " codec for $name\n") if DEBUG; | |||||||||||
| 82 | 1 | return $self->construct($name, ref $item, $args); | |||||||||||
| 83 | } | ||||||||||||
| 84 | else { | ||||||||||||
| 85 | 24 | ▶ | $self->debug("re-using cached codec for $name: $item\n") if $DEBUG; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 24 | return $item; | |||||||||||
| 87 | } | ||||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | |||||||||||||
| 91 | sub result { | ||||||||||||
| 92 | 50 | ▶ | my ($self, $name, $codec, $args) = @_; | ||||||||||
|
Subroutine
result: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 93 | # only cache codec objects created with no arguments | ||||||||||||
| 94 | 50 | ▶ | unless (@$args) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 49 | $self->debug("Caching $name codec for subsequent re-used: $codec") if DEBUG; | |||||||||||
| 96 | 49 | $self->{ codecs }->{ $name } = $codec; | |||||||||||
| 97 | } | ||||||||||||
| 98 | 50 | return $codec; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | |||||||||||||
| 102 | sub encode { | ||||||||||||
| 103 | 8 | ▶ | shift->codec(shift)->encode(@_); | ||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | |||||||||||||
| 107 | sub decode { | ||||||||||||
| 108 | 8 | ▶ | shift->codec(shift)->decode(@_); | ||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | |||||||||||||
| 112 | #----------------------------------------------------------------------- | ||||||||||||
| 113 | # export hooks | ||||||||||||
| 114 | #----------------------------------------------------------------------- | ||||||||||||
| 115 | |||||||||||||
| 116 | class->exports( | ||||||||||||
| 117 | hooks => { | ||||||||||||
| 118 | 15 | map { ($_ => \&_export_hook) } | |||||||||||
| 119 | qw( codec codecs ) | ||||||||||||
| 120 | } | ||||||||||||
| 121 | ); | ||||||||||||
| 122 | |||||||||||||
| 123 | |||||||||||||
| 124 | sub _export_hook { | ||||||||||||
| 125 | 15 | ▶ | my ($class, $target, $key, $symbols) = @_; | ||||||||||
|
Subroutine
_export_hook: called CC=2 SCAR=7.0
| |||||||||||||
| 126 | 15 | ▶ | croak "You didn't specify a value for the '$key' load option." | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | unless @$symbols; | ||||||||||||
| 128 | 15 | my $method = "export_$key"; | |||||||||||
| 129 | 15 | $class->$method($target, shift @$symbols); | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | |||||||||||||
| 133 | sub export_codec { | ||||||||||||
| 134 | 19 | ▶ | my ($class, $target, $name, $alias) = @_; | ||||||||||
|
Subroutine
export_codec: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 135 | 19 | my $codec = $class->codec($name); | |||||||||||
| 136 | 19 | ▶ | my $cmethod = $alias || CODEC_METHOD; | ||||||||||
|
Condition100%2/2
$alias || 'codec'
MC/DC100%1/1
$alias || 'codec'
$alias
Truth table100%2/2
A
A= $alias
| |||||||||||||
| 137 | 19 | ▶ | my $emethod = $alias ? join('_', ENCODE_METHOD, $alias) : ENCODE_METHOD; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 138 | 19 | ▶ | my $dmethod = $alias ? join('_', DECODE_METHOD, $alias) : DECODE_METHOD; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | 15 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 140 | |||||||||||||
| 141 | # prefix target class onto above method names | ||||||||||||
| 142 | 19 | $_= "${target}::$_" for ($cmethod, $emethod, $dmethod); | |||||||||||
| 143 | |||||||||||||
| 144 | 19 | ▶ | $class->debug("exporting $codec codec to $target\n") if $DEBUG; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | |||||||||||||
| 146 | # NOTE: I think it's more correct to attempt the export regardless of | ||||||||||||
| 147 | # any existing sub and allow a redefine warning to be raised. This is | ||||||||||||
| 148 | # better than silently failing to export the requested items. | ||||||||||||
| 149 | 19 | my $temp = $codec; # make sure this is a constant on 5.22 | |||||||||||
| 150 | 19 | ▶ | *{$cmethod} = sub() { $temp }; # unless defined &{$cmethod}; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||
| 151 | 19 | *{$emethod} = $codec->encoder; # unless defined &{$emethod}; | |||||||||||
| 152 | 19 | *{$dmethod} = $codec->decoder; # unless defined &{$dmethod}; | |||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | |||||||||||||
| 156 | sub export_codecs { | ||||||||||||
| 157 | 3 | ▶ | my ($class, $target, $names) = @_; | ||||||||||
|
Subroutine
export_codecs: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 158 | 3 | ▶ | if (ref $names eq HASH) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | 1 | ▶ | while (my ($key, $value) = each %$names) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | 1 | $class->export_codec($target, $value, $key); | |||||||||||
| 161 | } | ||||||||||||
| 162 | } | ||||||||||||
| 163 | else { | ||||||||||||
| 164 | 2 | ▶ | $names = [ split(DELIMITER, $names) ] unless ref $names eq ARRAY; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 165 | 2 | $class->export_codec($target, $_, $_) for @$names; | |||||||||||
| 166 | } | ||||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | 15 | 1; | |||||||||||
| 170 | |||||||||||||
| 171 | |||||||||||||
| 172 | __END__ | ||||||||||||