| 1 | package File::SOPS::Format::ENV::Ordered; | ||||||||||
| 2 | # ABSTRACT: the tied hash that keeps a flat format's parse in document order | ||||||||||
| 3 | 82 | our $VERSION = '0.005'; | |||||||||
| 4 | 82 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 82 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 82 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | ############################################################################### | ||||||||||
| 9 | # A ~15-line Tie::Hash is what "order preserving" means for a format whose | ||||||||||
| 10 | # parser has none: docs/adr/0036's condition 2 says a tied hash "and nothing | ||||||||||
| 11 | # else", because `keys` is what File::SOPS::_document_leaves walks. | ||||||||||
| 12 | # | ||||||||||
| 13 | # YAML::PP's own ordered hash (YAML::PP::Preserve::Hash) is not a separately | ||||||||||
| 14 | # loadable class and Tie::IxHash is not a prerequisite of this distribution, so | ||||||||||
| 15 | # this is the cheapest thing that meets the contract. Only STORE, FETCH, EXISTS | ||||||||||
| 16 | # and the iteration pair are exercised by the two handlers; the rest are here | ||||||||||
| 17 | # so that the tie is not a trap for a later caller. | ||||||||||
| 18 | # | ||||||||||
| 19 | # This module knows nothing about either format and loads nothing from this | ||||||||||
| 20 | # distribution -- Format::ENV and Format::INI load IT, and the dependency runs | ||||||||||
| 21 | # only that way. | ||||||||||
| 22 | # | ||||||||||
| 23 | # k158 moved it out of File/SOPS/Format/ENV.pm, where k36's | ||||||||||
| 24 | # one-new-file boundary put it, WITHOUT renaming it: both handlers already tie | ||||||||||
| 25 | # to this package name, and the move is meant to change nothing at all. It | ||||||||||
| 26 | # keeps ENV's namespace because that is the handler it was written for; INI | ||||||||||
| 27 | # uses it where it stands rather than growing a second copy, which is how the | ||||||||||
| 28 | # two would drift apart. | ||||||||||
| 29 | ############################################################################### | ||||||||||
| 30 | |||||||||||
| 31 | |||||||||||
| 32 | 33 | ▶ | sub TIEHASH { bless { order => [], value => {}, at => 0 }, $_[0] } | ||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 67 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: called CC=2 SCAR=7.0
| |||||||||||
| 34 | 67 | my ($self, $key, $value) = @_; | |||||||||
| 35 | 67 | ▶ | push @{ $self->{order} }, $key unless exists $self->{value}{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 67 | $self->{value}{$key} = $value; | |||||||||
| 37 | } | ||||||||||
| 38 | 107 | ▶ | sub FETCH { $_[0]->{value}{ $_[1] } } | ||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 94 | ▶ | sub EXISTS { exists $_[0]->{value}{ $_[1] } } | ||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 0 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||
| 41 | 0 | my ($self, $key) = @_; | |||||||||
| 42 | 0 | @{ $self->{order} } = grep { $_ ne $key } @{ $self->{order} }; | |||||||||
| 43 | 0 | delete $self->{value}{$key}; | |||||||||
| 44 | } | ||||||||||
| 45 | 0 | ▶ | sub CLEAR { $_[0]->{order} = []; $_[0]->{value} = {} } | ||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | 31 | ▶ | sub FIRSTKEY { $_[0]->{at} = 0; $_[0]->{order}[0] } | ||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 69 | ▶ | sub NEXTKEY { $_[0]->{order}[ ++$_[0]->{at} ] } | ||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 0 | ▶ | sub SCALAR { scalar @{ $_[0]->{order} } } | ||||||||
|
Subroutine
SCALAR: not called CC=1 SCAR=6.9
| |||||||||||
| 49 | |||||||||||
| 50 | 82 | 1; | |||||||||
| 51 | |||||||||||
| 52 | __END__ | ||||||||||