File: Resultset.pm

package info (click to toggle)
libcircle-be-perl 0.173320-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 388 kB
  • sloc: perl: 6,042; makefile: 2; sh: 1
file content (78 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#  You may distribute under the terms of the GNU General Public License
#
#  (C) Paul Evans, 2008-2010 -- leonerd@leonerd.org.uk

package Circle::Rule::Resultset;

use strict;
use warnings;

our $VERSION = '0.173320';

use Carp;

sub new
{
   my $class = shift;

   return bless {}, $class;
}

sub get_result
{
   my $self = shift;
   my ( $name ) = @_;

   carp "No result '$name'" unless exists $self->{$name};

   return $self->{$name};
}

sub push_result
{
   my $self = shift;
   my ( $name, $value ) = @_;

   if( !exists $self->{$name} ) {
      $self->{$name} = [ $value ];
   }
   elsif( ref $self->{$name} eq "ARRAY" ) {
      push @{ $self->{$name} }, $value;
   }
   else {
      croak "Expected '$name' to be an ARRAY result";
   }
}

sub merge_from
{
   my $self = shift;
   my ( $other ) = @_;

   foreach my $name ( %$other ) {
      my $otherval = $other->{$name};

      if( !$self->{$name} ) {
         $self->{$name} = $otherval;
         next;
      }

      my $myval = $self->{$name};

      # Already had it - type matches?
      if( ref $myval ne ref $otherval ) {
         croak "Cannot merge; '$name' has different result types";
      }

      my $type = ref $myval;

      if( ref $myval eq "ARRAY" ) {
         push @$myval, @$otherval;
      }
      else {
         croak "Don't know how to handle result type '$name' ($type)";
      }
   }
}

0x55AA;