File Coverage

lib/Rex/Inventory/DMIDecode/Section.pm
Criterion Covered Total %
statement 68 78 87.1
branch 7 10 70.0
condition 1 3 33.3
subroutine 15 16 93.7
pod 0 8 0.0
total 91 115 79.1


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Inventory::DMIDecode::Section;
6              
7 39     39   509 use v5.12.5;
  39         150  
8 39     39   200 use warnings;
  39         90  
  39         1774  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12             require Exporter;
13 39     39   688 use Symbol;
  39         1114  
  39         2867  
14 39     39   272 use base qw(Exporter);
  39         101  
  39         3378  
15 39     39   293 use vars qw($SECTION @EXPORT);
  39         158  
  39         18604  
16              
17             @EXPORT = qw(section);
18             $SECTION = {};
19              
20             sub new {
21 69     69 0 204 my $that = shift;
22 69   33     585 my $proto = ref($that) || $that;
23 69         540 my $self = {@_};
24              
25 69         382 bless( $self, $proto );
26              
27 69         287 return $self;
28             }
29              
30             sub section {
31 234     234 0 667 my ( $class, $section ) = @_;
32 234         763 $SECTION->{$class} = $section;
33             }
34              
35             sub has {
36 234     234 0 601 my ( $class, $item, $is_array ) = @_;
37              
38 234 50       864 unless ( ref($item) eq "ARRAY" ) {
39 0         0 my $_tmp = $item;
40 0         0 $item = [$_tmp];
41             }
42              
43 234         389 for my $itm ( @{$item} ) {
  234         583  
44 1365         2125 my $o_itm = $itm;
45 1365         4653 $itm =~ s/[^a-zA-Z0-9_]+/_/g;
46 1365         4517 my $ref_to_item_getter = qualify_to_ref( "get_\L$itm", $class );
47 1365         2643 *{$ref_to_item_getter} = sub {
48 82     82   2843 my $self = shift;
49 82         718 return $self->get( $o_itm, $is_array );
50 1365         26897 };
51              
52 1365         2746 my $ref_to_items = qualify_to_ref( 'items', $class );
53 1365         20606 push( @{ *{$ref_to_items} }, "\L$itm" );
  1365         1729  
  1365         5123  
54             }
55             }
56              
57             sub dmi {
58              
59 113     113 0 306 my ($self) = @_;
60 113         937 return $self->{"dmi"};
61              
62             }
63              
64             sub get {
65              
66 82     82 0 408 my ( $self, $key, $is_array ) = @_;
67 82         687 return $self->_search_for( $key, $is_array );
68              
69             }
70              
71             sub get_all {
72              
73 1     1 0 12 my ($self) = @_;
74              
75 39     39   335 use Data::Dumper;
  39         84  
  39         16613  
76 1         2 my $r = ref($self);
77              
78 1         8 my $ref_to_items = qualify_to_ref( 'items', $r );
79 1         29 my @items = @{ *{$ref_to_items} };
  1         2  
  1         7  
80              
81 1         2 my $ret = {};
82 1         3 for my $itm (@items) {
83 4         14 my $f = "get_$itm";
84 4         13 $ret->{$itm} = $self->$f();
85             }
86              
87 1         10 return $ret;
88              
89             }
90              
91             sub dump {
92              
93 0     0 0 0 my ($self) = @_;
94              
95 0         0 require Data::Dumper;
96             print Data::Dumper::Dumper(
97 0         0 $self->dmi->get_tree( $SECTION->{ ref($self) } ) );
98              
99             }
100              
101             sub _search_for {
102 82     82   303 my ( $self, $key, $is_array ) = @_;
103              
104 82 100       454 unless ( $self->dmi->get_tree( $SECTION->{ ref($self) } ) ) {
105              
106             #die $SECTION->{ref($self)} . " not supported";
107 51         3071 return;
108             }
109              
110 31         53 my $idx = 0;
111 31         42 for my $entry ( @{ $self->dmi->get_tree( $SECTION->{ ref($self) } ) } ) {
  31         53  
112 31         46 my ($_key) = keys %{$entry};
  31         106  
113 31 100       76 if ($is_array) {
114 9 50       22 if ( $idx != $self->get_index() ) {
115 0         0 ++$idx;
116 0         0 next;
117             }
118             }
119              
120 31 50       73 if ( exists $entry->{$key} ) {
121 31         170 return $entry->{$key};
122             }
123             else {
124 0         0 return "";
125             }
126 0         0 ++$idx;
127             }
128              
129 0         0 return "";
130             }
131              
132             sub get_index {
133              
134 9     9 0 20 my ($self) = @_;
135 9         20 return $self->{"index"};
136              
137             }
138              
139             1;