Debian Codegraph @

README

raw
1 NAME
2
3 Class::Type::Enum - Build Enum-like classes
4
5 VERSION
6
7 version 0.014
8
9 SYNOPSIS
10
11 package Toast::Status {
12 use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt'];
13 }
14
15 package Toast {
16 use Moo;
17
18 has status => (
19 is => 'rw',
20 isa => Toast::Status->type_constraint,
21 coerce => 1,
22 handles => [ Toast::Status->list_is_methods ],
23 );
24 }
25
26 my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast );
27
28 my @trashcan = grep { $_->is_burnt } @toast;
29 my @plate = grep { $_->is_toast } @toast;
30
31 my $ready_status = Toast::Status->new('toast');
32 my @eventual_toast = grep { $_->status < $ready_status } @toast;
33
34 # or:
35
36 @eventual_toast = grep { $_->status lt 'toast' } @toast;
37
38 # or:
39
40 @eventual_toast = grep { $_->status->none('toast', 'burnt') } @toast;
41
42 DESCRIPTION
43
44 Class::Type::Enum is a class builder for type-like classes to represent
45 your enumerated values. In particular, it was built to scratch an itch
46 with DBIx::Class value inflation.
47
48 I wouldn't consider the interface stable yet; I'd love feedback on this
49 dist.
50
51 When useing Class::Type::Enum:
52
53 * Required:
54
55 values => [@symbols]
56
57 The list of symbolic values in your enum, in ascending order if
58 relevant.
59
60 values => {symbol => ordinal, ...}
61
62 The list of symbols and ordinal values in your enum. There is no
63 check that a given ordinal isn't reused.
64
65 Custom Ordinal Values
66
67 If you'd like to build an enum that works like a bitfield or some other
68 custom setup, you need only pass a more explicit hashref to
69 Class::Type::Enum.
70
71 package BitField {
72 use Class::Type::Enum values => {
73 READ => 1,
74 WRITE => 2,
75 EXECUTE => 4,
76 };
77 }
78
79 METHODS
80
81 $class->import(values => ...)
82
83 Sets up the consuming class as a subclass of Class::Type::Enum and
84 installs functions that are unique to the class.
85
86 $class->new($value)
87
88 Your basic constructor, expects only a value corresponding to a symbol
89 in the enum type. Also works as an instance method for enums of the
90 same class.
91
92 $class->inflate_symbol($symbol)
93
94 Does the actual work of $class->new($value), also used when inflating
95 values for DBIx::Class::InflateColumn::ClassTypeEnum.
96
97 $class->inflate_ordinal($ord)
98
99 Used when inflating ordinal values for
100 DBIx::Class::InflateColumn::ClassTypeEnum or if you need to work with
101 ordinals directly.
102
103 $class->sym_to_ord
104
105 Returns a hashref keyed by symbol, with ordinals as values.
106
107 $class->ord_to_sym
108
109 Returns a hashref keyed by ordinal, with symbols as values.
110
111 $class->values
112
113 Returns an arrayref of valid symbolic values, in order.
114
115 $class->list_is_methods
116
117 Returns a list of is_ methods defined for each symbolic value for the
118 class.
119
120 $class->type_constraint
121
122 This method requires the optional dependency Type::Tiny.
123
124 Returns a type constraint suitable for use with Moo and friends.
125
126 $class->test_symbol($value)
127
128 Test whether or not the given value is a valid symbol in this enum
129 class.
130
131 $class->test_ordinal($value)
132
133 Test whether or not the given value is a valid ordinal in this enum
134 class.
135
136 $class->coerce_symbol($value)
137
138 If the given value is already a $class, return it, otherwise try to
139 inflate it as a symbol. Dies on invalid value.
140
141 $class->coerce_ordinal($value)
142
143 If the given value is already a $class, return it, otherwise try to
144 inflate it as an ordinal. Dies on invalid value.
145
146 $class->coerce_any($value)
147
148 If the given value is already a $class, return it, otherwise try to
149 inflate it first as an ordinal, then as a symbol. Dies on invalid
150 value.
151
152 $o->is($value)
153
154 Given a test symbol, test that the enum instance's value is equivalent.
155
156 An exception is thrown if an invalid symbol is provided
157
158 $o->is_$value
159
160 Shortcut for $o->is($value)
161
162 $o->stringify
163
164 Returns the symbolic value.
165
166 $o->numify
167
168 Returns the ordinal value.
169
170 $o->cmp($other, $reversed = undef)
171
172 The string-compare implementation used by overloading. Returns the same
173 values as cmp. The optional third argument is an artifact of overload,
174 set to true if the order of $o and $other have been reversed in order
175 to make the overloaded method call work.
176
177 $o->any(@cases)
178
179 True if $o->is(..) for any of the given cases.
180
181 $o->none(@cases)
182
183 True if $o->is(..) for none of the given cases.
184
185 SEE ALSO
186
187 * Object::Enum
188
189 * Class::Enum
190
191 * Enumeration
192
193 AUTHOR
194
195 Meredith Howard <[email protected]>
196
197 COPYRIGHT AND LICENSE
198
199 This software is copyright (c) 2018 by Meredith Howard.
200
201 This is free software; you can redistribute it and/or modify it under
202 the same terms as the Perl 5 programming language system itself.
203