| 1 | package eris::role::dictionary::hash; | ||||||||||
| 2 | # ABSTRACT: Simple dictionary implementation based off a hash | ||||||||||
| 3 | |||||||||||
| 4 | 5 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use JSON::MaybeXS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 5 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 5 | with qw(eris::role::dictionary); | |||||||||
| 8 | |||||||||||
| 9 | 5 | our $VERSION = '0.008'; # VERSION | |||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | 5 | requires qw(hash); | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | sub lookup { | ||||||||||
| 16 | 0 | ▶ | my ($self,$field) = @_; | ||||||||
|
Subroutine
lookup: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 17 | |||||||||||
| 18 | 0 | my $entry = undef; | |||||||||
| 19 | 0 | my $dict = $self->hash; | |||||||||
| 20 | 0 | ▶ | if( exists $dict->{$field} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | $entry = { | ||||||||||
| 22 | field => $field, | ||||||||||
| 23 | type => 'keyword', | ||||||||||
| 24 | 0 | ref $dict->{$field} eq 'HASH' ? %{ $dict->{$field} } | |||||||||
| 25 | 0 | ▶ | : ( description => $dict->{$field} ), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | }; | ||||||||||
| 27 | } | ||||||||||
| 28 | 0 | return $entry; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | sub fields { | ||||||||||
| 33 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
fields: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | 0 | return [ sort keys %{ $self->hash } ]; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | |||||||||||
| 38 | sub expand_line { | ||||||||||
| 39 | 0 | ▶ | my ($self,$line) = @_; | ||||||||
|
Subroutine
expand_line: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 40 | |||||||||||
| 41 | 0 | my ($k,$v); | |||||||||
| 42 | 0 | ▶ | if( $line =~ /^{/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | eval { | ||||||||||
| 44 | 0 | my $field = decode_json($line); | |||||||||
| 45 | 0 | $k = lc delete $field->{name}; | |||||||||
| 46 | 0 | $v = $field; | |||||||||
| 47 | 0 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | my $err = $@; | |||||||||
| 49 | 0 | warn "BAD JSON: $err\n\n$line\n"; | |||||||||
| 50 | }; | ||||||||||
| 51 | } | ||||||||||
| 52 | else { | ||||||||||
| 53 | 0 | my ($field,$desc) = split /\s+/, $line, 2; | |||||||||
| 54 | 0 | $k = lc $field; | |||||||||
| 55 | 0 | $v = $desc; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | return $k ? ( $k => $v ) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 5 | 1; | |||||||||
| 63 | |||||||||||
| 64 | __END__ | ||||||||||
| 65 | |||||||||||
| 66 | =pod | ||||||||||
| 67 | |||||||||||
| 68 | =encoding UTF-8 | ||||||||||
| 69 | |||||||||||
| 70 | =head1 NAME | ||||||||||
| 71 | |||||||||||
| 72 | eris::role::dictionary::hash - Simple dictionary implementation based off a hash | ||||||||||
| 73 | |||||||||||
| 74 | =head1 VERSION | ||||||||||
| 75 | |||||||||||
| 76 | version 0.008 | ||||||||||
| 77 | |||||||||||
| 78 | =head1 SYNOPSIS | ||||||||||
| 79 | |||||||||||
| 80 | Simplest possible dictionary implementation | ||||||||||
| 81 | |||||||||||
| 82 | package my::app::dictionary::business; | ||||||||||
| 83 | |||||||||||
| 84 | use Moo; | ||||||||||
| 85 | with qw( | ||||||||||
| 86 | eris::role::dictionary::hash | ||||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | sub hash { | ||||||||||
| 90 | return { | ||||||||||
| 91 | 'customer_id' => "Our customer ID field", | ||||||||||
| 92 | 'store_id' => "Our store ID field", | ||||||||||
| 93 | 'price_usd' => "Object price in USD", | ||||||||||
| 94 | 'transaction_key' => "Transaction Identifier", | ||||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | =head1 METHODS | ||||||||||
| 99 | |||||||||||
| 100 | =head2 lookup($field) | ||||||||||
| 101 | |||||||||||
| 102 | Find the field in the hash, returns a hashref in the format: | ||||||||||
| 103 | |||||||||||
| 104 | { | ||||||||||
| 105 | field => $field, | ||||||||||
| 106 | description => $lookup_hash{$field}, | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | Or if the hash value is a hash reference, we return: | ||||||||||
| 110 | |||||||||||
| 111 | { | ||||||||||
| 112 | field => $field, | ||||||||||
| 113 | %{ $lookup_hash{$field} }, | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | =head2 fields() | ||||||||||
| 117 | |||||||||||
| 118 | Returns the sorted list of keys in the lookup hash | ||||||||||
| 119 | |||||||||||
| 120 | =head2 expand_line | ||||||||||
| 121 | |||||||||||
| 122 | Expand a line in a file/DATA section to a workable hash | ||||||||||
| 123 | |||||||||||
| 124 | =head1 INTERFACE | ||||||||||
| 125 | |||||||||||
| 126 | =head2 hash | ||||||||||
| 127 | |||||||||||
| 128 | Return a HashRef with the field names as keys and a string description of the field. | ||||||||||
| 129 | |||||||||||
| 130 | May also return a HashRef with field names as keys and a HashRef as a value. Those key/value | ||||||||||
| 131 | pairs will be returned to the C<lookup()> function. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 SEE ALSO | ||||||||||
| 134 | |||||||||||
| 135 | L<eris::role::dictionary>, L<eris::dictionary>, L<eris::dictionary::cee> | ||||||||||
| 136 | |||||||||||
| 137 | =head1 AUTHOR | ||||||||||
| 138 | |||||||||||
| 139 | Brad Lhotsky <brad@divisionbyzero.net> | ||||||||||
| 140 | |||||||||||
| 141 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 142 | |||||||||||
| 143 | This software is Copyright (c) 2015 by Brad Lhotsky. | ||||||||||
| 144 | |||||||||||
| 145 | This is free software, licensed under: | ||||||||||
| 146 | |||||||||||
| 147 | The (three-clause) BSD License | ||||||||||
| 148 | |||||||||||
| 149 | =cut | ||||||||||