File Coverage

blib/lib/Astro/STSDAS/Table/Columns.pm
Criterion Covered Total %
statement 60 72 83.3
branch 12 18 66.6
condition 2 3 66.6
subroutine 16 20 80.0
pod 11 11 100.0
total 101 124 81.4


line stmt bran cond sub pod time code
1             package Astro::STSDAS::Table::Columns;
2              
3             require 5.005_62;
4 3     3   27375 use strict;
  3         7  
  3         107  
5 3     3   15 use warnings;
  3         7  
  3         83  
6              
7 3     3   15 use Carp;
  3         11  
  3         240  
8              
9 3     3   1653 use Astro::STSDAS::Table::Column;
  3         8  
  3         2518  
10              
11             our $VERSION = '0.01';
12              
13             sub new
14             {
15 3     3 1 19 my $class = shift;
16 3   66     19 $class = ref($class) || $class;
17              
18              
19 3         16 my $self = {
20             cols => {},
21             idxs => {},
22             idxs_s => undef, # sorted indices
23             };
24              
25 3         17 bless $self, $class;
26             }
27              
28             sub ncols
29             {
30 7     7 1 2062 scalar keys %{$_[0]->{cols}};
  7         49  
31             }
32              
33             sub add
34             {
35 4     4 1 1029 my $self = shift;
36              
37 4         23 $self->_add( Astro::STSDAS::Table::Column->new( @_ ) );
38             }
39              
40             sub _add
41             {
42 7     7   12 my ( $self, $col ) = @_;
43              
44 7 100       23 croak( __PACKAGE__, "->add: duplicate column index `", $col->idx, "'\n" )
45             if exists $self->{idxs}{$col->idx};
46              
47 6 100       24 croak( __PACKAGE__, "->add: duplicate column name `", $col->name, "'\n" )
48             if exists $self->{cols}{lc $col->name};
49              
50 5         20 $self->{cols}{lc $col->name} = $col;
51 5         18 $self->{idxs}{$col->idx} = $col;
52 5         11 $_[0]->{idxs_s} = undef;
53 5         17 $col;
54             }
55              
56             sub del
57             {
58 1     1 1 3 my $self = shift;
59 1         3 my $col = shift;
60              
61             # first make sure its one of ours.
62 1 50       3 return 0 unless grep { $col == $_ } values %{$self->{cols}};
  2         11  
  1         5  
63              
64 1         4 $self->delbyname( $col->name );
65             }
66              
67             sub delbyname
68             {
69 4     4 1 18 my $self = shift;
70 4         9 my $name = lc shift;
71 4 100       17 return 0 unless exists $self->{cols}{$name};
72              
73 3         13 delete $self->{idxs}{$self->{cols}{$name}->idx};
74 3         8 delete $self->{cols}{$name};
75 3         17 $_[0]->{idxs_s} = undef;
76 3         14 1;
77             }
78              
79             sub byidx
80             {
81 5 100   5 1 43 exists $_[0]->{idxs}{$_[1]} ? $_[0]->{idxs}{$_[1]} : undef;
82             }
83              
84              
85             sub byname
86             {
87 7     7 1 18 my $name = lc $_[1];
88 7 100       31 return undef unless exists $_[0]->{cols}{$name};
89 6         26 $_[0]->{cols}{$name};
90             }
91              
92             sub cols
93             {
94 0 0   0 1 0 $_[0]->_mkidxs_s unless defined $_[0]->{idxs_s};
95 0         0 @{$_[0]->{idxs_s}};
  0         0  
96             }
97              
98             sub _mkidxs_s
99             {
100 0     0   0 $_[0]->{idxs_s} = [ sort { $a->idx <=> $b->idx} values %{$_[0]->{cols}} ];
  0         0  
  0         0  
101             }
102              
103             sub names
104             {
105 0     0 1 0 map { $_->name } $_[0]->cols;
  0         0  
106             }
107              
108             sub rename
109             {
110 1     1 1 3 my ( $self, $name, $newname ) = @_;
111              
112 1         3 my $col = $self->byname($name);
113 1 50       5 return undef unless defined $col;
114              
115 1         4 $self->delbyname( $name );
116              
117 1         4 $col->name($newname);
118 1         5 $self->_add( $col );
119             }
120              
121             sub _access
122             {
123 27     27   46 my $what = shift;
124            
125             sub {
126            
127 0     0   0 my $self = shift;
128 0         0 my $name = lc shift;
129 0 0       0 return undef unless exists $self->{cols}{$name};
130 0         0 $self->{cols}{$name}->$what(@_);
131             }
132 27         174 }
133              
134             {
135 3     3   103 no strict 'refs';
  3         7  
  3         433  
136             *$_ = _access( $_ )
137             foreach qw( units format idx offset type nelem fmt is_string is_undef );
138             }
139              
140              
141              
142             sub copy
143             {
144 1     1 1 2 my $self = shift;
145              
146 1         5 my $new = $self->new;
147              
148 1         2 $new->_add( $_->copy ) foreach values %{$self->{cols}};
  1         10  
149              
150 1         4 $new;
151             }
152              
153              
154             1;
155             __END__