File Coverage

blib/lib/MogileFS/Factory/Class.pm
Criterion Covered Total %
statement 12 63 19.0
branch 0 6 0.0
condition n/a
subroutine 4 15 26.6
pod 0 10 0.0
total 16 94 17.0


line stmt bran cond sub pod time code
1             package MogileFS::Factory::Class;
2 21     21   117 use strict;
  21         47  
  21         694  
3 21     21   114 use warnings;
  21         59  
  21         551  
4 21     21   115 use base 'MogileFS::Factory';
  21         44  
  21         2024  
5              
6 21     21   16160 use MogileFS::Class;
  21         65  
  21         14055  
7              
8             # This class is a reimplementation since classids and classnames
9             # are not globally unique... uses the same interface.
10             # Stupid/wasteful.
11             sub set {
12 0     0 0   my ($self, $args) = @_;
13 0           my $domain_factory = MogileFS::Factory::Domain->get_factory;
14              
15 0           my $class = MogileFS::Class->new_from_args($args, $domain_factory);
16 0           my $dmid = $class->dmid;
17 0           $self->{by_id}->{$dmid}->{$class->id} = $class;
18 0           $self->{by_name}->{$dmid}->{$class->name} = $class;
19 0           return $class;
20             }
21              
22             # Class factory is very awkward. Lets be very flexible in what we take; a
23             # domain object + id, a dmid, or a string with dmid-classid.
24             sub _find_ids {
25 0     0     my $self = shift;
26 0           my $dom = shift;
27 0 0         my $dmid = ref $dom ? $dom->id : $dom;
28 0 0         if ($dmid =~ m/^(\d+)-(\d+)$/) {
29 0           return $1, $2;
30             }
31 0           return $dmid, @_;
32             }
33              
34             # Example of what we could use for testing.
35             # Test creates the object, injects its own factory, then hands it to us.
36             sub set_from_obj {
37 0     0 0   my ($self, $obj) = @_;
38             }
39              
40             sub remove {
41 0     0 0   my $self = shift;
42 0           my $class = shift;
43 0           my $domid = $class->dmid;
44 0           my $clsid = $class->id;
45 0 0         if (exists $self->{by_id}->{$domid}->{$clsid}) {
46 0           delete $self->{by_id}->{$domid}->{$clsid};
47 0           delete $self->{by_name}->{$domid}->{$class->name};
48             }
49             }
50              
51             sub get_by_id {
52 0     0 0   my $self = shift;
53 0           my ($dmid, $id) = $self->_find_ids(@_);
54 0           return $self->{by_id}->{$dmid}->{$id};
55             }
56              
57             sub get_by_name {
58 0     0 0   my $self = shift;
59 0           my ($dmid, $name) = $self->_find_ids(@_);
60 0           return $self->{by_name}->{$dmid}->{$name};
61             }
62              
63             sub get_ids {
64 0     0 0   my $self = shift;
65 0           my ($dmid) = $self->_find_ids(@_);
66 0           return keys %{$self->{by_id}->{$dmid}};
  0            
67             }
68              
69             sub get_names {
70 0     0 0   my $self = shift;
71 0           my ($dmid) = $self->_find_ids(@_);
72 0           return keys %{$self->{by_name}->{$dmid}};
  0            
73             }
74              
75             sub get_all {
76 0     0 0   my $self = shift;
77 0           my ($dmid) = $self->_find_ids(@_);
78 0           return values %{$self->{by_id}->{$dmid}};
  0            
79             }
80              
81             sub map_by_id {
82 0     0 0   my $self = shift;
83 0           my ($dmid) = $self->_find_ids(@_);
84 0           my $set = $self->{by_id}->{$dmid};
85 0           return { map { $_ => $set->{$_} } keys %{$set} };
  0            
  0            
86             }
87              
88             sub map_by_name {
89 0     0 0   my $self = shift;
90 0           my ($dmid) = $self->_find_ids(@_);
91 0           my $set = $self->{by_name}->{$dmid};
92 0           return { map { $_ => $set->{$_} } keys %{$set} };
  0            
  0            
93             }
94              
95             1;