File Coverage

blib/lib/Thrift/Parser/Type/Enum.pm
Criterion Covered Total %
statement 28 36 77.7
branch 9 12 75.0
condition n/a
subroutine 8 11 72.7
pod 7 7 100.0
total 52 66 78.7


line stmt bran cond sub pod time code
1             package Thrift::Parser::Type::Enum;
2              
3             =head1 NAME
4              
5             Thrift::Parser::Type::Enum - Enum type
6              
7             =head1 DESCRIPTION
8              
9             =cut
10              
11 6     6   35 use strict;
  6         11  
  6         193  
12 6     6   29 use warnings;
  6         11  
  6         147  
13 6     6   30 use base qw(Thrift::Parser::Type);
  6         11  
  6         453  
14 6     6   33 use Scalar::Util qw(blessed);
  6         12  
  6         3567  
15              
16             =head1 METHODS
17              
18             This class inherits from L; see docs there for inherited methods.
19              
20             =cut
21              
22             sub type_id {
23 0     0 1 0 return Thrift::Parser::Types->to_id('i32');
24             }
25              
26             =head2 compose
27              
28             Pass either a named element of the enumeration or an index:
29              
30             my $value = $enum->compose('PHP');
31             my $value = $enum->compose('_4');
32              
33             As you can see, the index is '_' + digits. Throws L
34              
35             =cut
36              
37             sub compose {
38 4     4 1 1685 my ($class, $value) = @_;
39              
40 4 50       26 if (blessed $value) {
41 0 0       0 if (! $value->isa($class)) {
42 0         0 Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value));
43             }
44 0         0 return $value;
45             }
46              
47 4 100       26 Thrift::Parser::InvalidArgument->throw("'undef' is not valid for $class") if ! defined $value;
48 3 100       13 if (my ($id) = $value =~ m{^_(\d+)$}) {
49 1         4 return $class->new_from_id($id);
50             }
51             else {
52 2         10 return $class->new_from_name($value);
53             }
54             }
55              
56             sub write {
57 0     0 1 0 my ($self, $output) = @_;
58 0         0 $output->writeI32($self->value);
59             }
60              
61             =head2 new_from_id
62              
63             Same as L when called with '_' + digit.
64              
65             =cut
66              
67             sub new_from_id {
68 3     3 1 3548 my ($class, $id) = @_;
69 3         16 my $name = $class->idl->value_id_name($id);
70 3 100       16 Thrift::Parser::InvalidArgument->throw(
71             error => "No value found for enum index '$id' in type '".$class->name."'",
72             key => 'id', value => $id,
73             ) if ! defined $name;
74 2         21 return $class->new({ value => $id });
75             }
76              
77             =head2 new_from_name
78              
79             Same as L when called with an element name.
80              
81             =cut
82              
83             sub new_from_name {
84 4     4 1 3578 my ($class, $name) = @_;
85 4         16 my $id = $class->idl->value_named($name);
86 4 100       17 Thrift::Parser::InvalidArgument->throw(
87             error => "No value found for enum index '$name' in type '".$class->name."'",
88             key => 'name', value => $name,
89             ) if ! defined $id;
90 3         18 return $class->new({ value => $id });
91             }
92              
93             =head2 value_name
94              
95             my $object = $enum->compose('PHP');
96             $object->value_name == 'PHP';
97              
98             Returns the name of the element referenced by $self.
99              
100             =cut
101              
102             sub value_name {
103 3     3 1 42 my $self = shift;
104 3         10 return $self->idl->value_id_name($self->value);
105             }
106              
107             sub value_plain {
108 0     0 1   my $self = shift;
109 0           return $self->value_name;
110             }
111              
112             =head1 COPYRIGHT
113              
114             Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
115              
116             The full text of the license can be found in the LICENSE file included with this module.
117              
118             =head1 AUTHOR
119              
120             Eric Waters
121              
122             =cut
123              
124             1;