Class-Type-Enum-0.014000755001750001750 013365151052 14672 5ustar00mhowardmhoward000000000000README100644001750001750 1155613365151052 15663 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014NAME Class::Type::Enum - Build Enum-like classes VERSION version 0.014 SYNOPSIS package Toast::Status { use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt']; } package Toast { use Moo; has status => ( is => 'rw', isa => Toast::Status->type_constraint, coerce => 1, handles => [ Toast::Status->list_is_methods ], ); } my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast ); my @trashcan = grep { $_->is_burnt } @toast; my @plate = grep { $_->is_toast } @toast; my $ready_status = Toast::Status->new('toast'); my @eventual_toast = grep { $_->status < $ready_status } @toast; # or: @eventual_toast = grep { $_->status lt 'toast' } @toast; # or: @eventual_toast = grep { $_->status->none('toast', 'burnt') } @toast; DESCRIPTION Class::Type::Enum is a class builder for type-like classes to represent your enumerated values. In particular, it was built to scratch an itch with DBIx::Class value inflation. I wouldn't consider the interface stable yet; I'd love feedback on this dist. When useing Class::Type::Enum: * Required: values => [@symbols] The list of symbolic values in your enum, in ascending order if relevant. values => {symbol => ordinal, ...} The list of symbols and ordinal values in your enum. There is no check that a given ordinal isn't reused. Custom Ordinal Values If you'd like to build an enum that works like a bitfield or some other custom setup, you need only pass a more explicit hashref to Class::Type::Enum. package BitField { use Class::Type::Enum values => { READ => 1, WRITE => 2, EXECUTE => 4, }; } METHODS $class->import(values => ...) Sets up the consuming class as a subclass of Class::Type::Enum and installs functions that are unique to the class. $class->new($value) Your basic constructor, expects only a value corresponding to a symbol in the enum type. Also works as an instance method for enums of the same class. $class->inflate_symbol($symbol) Does the actual work of $class->new($value), also used when inflating values for DBIx::Class::InflateColumn::ClassTypeEnum. $class->inflate_ordinal($ord) Used when inflating ordinal values for DBIx::Class::InflateColumn::ClassTypeEnum or if you need to work with ordinals directly. $class->sym_to_ord Returns a hashref keyed by symbol, with ordinals as values. $class->ord_to_sym Returns a hashref keyed by ordinal, with symbols as values. $class->values Returns an arrayref of valid symbolic values, in order. $class->list_is_methods Returns a list of is_ methods defined for each symbolic value for the class. $class->type_constraint This method requires the optional dependency Type::Tiny. Returns a type constraint suitable for use with Moo and friends. $class->test_symbol($value) Test whether or not the given value is a valid symbol in this enum class. $class->test_ordinal($value) Test whether or not the given value is a valid ordinal in this enum class. $class->coerce_symbol($value) If the given value is already a $class, return it, otherwise try to inflate it as a symbol. Dies on invalid value. $class->coerce_ordinal($value) If the given value is already a $class, return it, otherwise try to inflate it as an ordinal. Dies on invalid value. $class->coerce_any($value) If the given value is already a $class, return it, otherwise try to inflate it first as an ordinal, then as a symbol. Dies on invalid value. $o->is($value) Given a test symbol, test that the enum instance's value is equivalent. An exception is thrown if an invalid symbol is provided $o->is_$value Shortcut for $o->is($value) $o->stringify Returns the symbolic value. $o->numify Returns the ordinal value. $o->cmp($other, $reversed = undef) The string-compare implementation used by overloading. Returns the same values as cmp. The optional third argument is an artifact of overload, set to true if the order of $o and $other have been reversed in order to make the overloaded method call work. $o->any(@cases) True if $o->is(..) for any of the given cases. $o->none(@cases) True if $o->is(..) for none of the given cases. SEE ALSO * Object::Enum * Class::Enum * Enumeration AUTHOR Meredith Howard COPYRIGHT AND LICENSE This software is copyright (c) 2018 by Meredith Howard. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. LICENSE100644001750001750 4366613365151052 16017 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014This software is copyright (c) 2018 by Meredith Howard. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2018 by Meredith Howard. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2018 by Meredith Howard. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Changes100644001750001750 615613365151052 16256 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.0140.014 2018-10-27T21:01:28 - Auto generate META.json using plugin [MetaJSON]. (Mohammad S Anwar) 0.013 2018-10-23T06:42:24Z - Fix META repository (Meredith Howard) 0.012 2018-10-23T03:50:03Z - Fix next::method call (Meredith Howard) 0.011 2018-07-04T02:15:46Z - Fix up prereqs (Meredith Howard) 0.010 2018-07-03T03:58:32Z - Add type_constraint - Resolves #5 (Thanks TOBYINK!) (Meredith Howard) - Add list_is_methods - Resolves #4 (Thanks TOBYINK!) (Meredith Howard) - Do away with hashref representation (Meredith Howard) - Make 5.8 compatible - Resolves #3 (Thanks TOBYINK!) (Meredith Howard) 0.009 2017-04-02T19:23:37Z - Switch to croaks so entry points are more apparent (Meredith Howard) - Make string comparison work in terms of the enumeration (Meredith Howard) 0.008 2017-04-02T03:26:26Z - Fix inflate/deflate under strict arg checks (Meredith Howard) - Use metacpan url on github (Meredith Howard) 0.007 2017-04-02T01:55:08Z - Update for Function::Parameters 2 (Meredith Howard) 0.006 2016-03-06T07:46:43Z - Fix DBIC inflation. (Meredith Howard) 0.005 2015-12-23T08:51:56Z - Fixed broken link in the pod of package Class::Type::Enum. (Mohammad S Anwar) 0.004 2015-12-22T00:24:03Z - Some testers seem confused about minimum version using `v1.33` (Meredith Howard) - Add test for case sorta like the synopsis (Meredith Howard) - Add 00-load-all (Meredith Howard) 0.003 2015-11-09T10:25:12Z - Fix up Moo example (Meredith Howard) 0.002 2015-11-09T10:15:13Z - Require List::Util min 1.33 (Meredith Howard) - Clarify is() doc (Meredith Howard) - Add more interface checks (Meredith Howard) - Drop methods that return functions. Create test_{ordinal,symbol} and coerce_{ordinal,symbol,any} (Meredith Howard) - Rename inflate* to inflate_symbol and inflate_ordinal (Meredith Howard) - Rename extra option enum_is_ord to enum_ordinal_storage (Meredith Howard) - Rename to sym_to_ord and ord_to_sym; move toward better use of terms. (Meredith Howard) - Drop init and bits options as unnecessary, document values hashref option. (Meredith Howard) - Allow ->new on instances (Meredith Howard) 0.001 2015-10-25T02:37:10Z - Add empty Changes (Meredith Howard) - add stability / feedback note to pod (Meredith Howard) - Mention DBIC again (Meredith Howard) - Readme updates (Meredith Howard) - Fix up some links, add see-also sections (Meredith Howard) - Add docs! (Meredith Howard) - Support inflate from ordinal (Meredith Howard) - add .gitignore (Meredith Howard) - Add dist.init and weaver.ini (Meredith Howard) - Switch to a good ol' hashref, as DBIC doesn't try deflating scalar refs (Meredith Howard) - Update readme (Meredith Howard) - Add DBIx::Class::InflateColumn::ClassTypeEnum.pm (Meredith Howard) - use the term 'ord' for ordinal rather than 'raw' (Meredith Howard) - Add any and none (Meredith Howard) - Rename to Class::Type::Enum (Meredith Howard) - Drop Moo::Role and ::Parameterized, install into the caller with C::M::M (Meredith Howard) - Test that we can use the role more than once (Meredith Howard) - Add code, test, and a readme note (Meredith Howard) - init (Meredith Howard) dist.ini100640001750001750 105213365151052 16411 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014name = Class-Type-Enum author = Meredith Howard license = Perl_5 copyright_holder = Meredith Howard [@GitHub] metacpan = 1 [MetaJSON] [@Git] [Git::NextVersion] [Git::GatherDir] exclude_filename = Changes [@Filter] -bundle = @Basic -remove = Readme -remove = GatherDir [AutoPrereqs] skip = ^Types::Standard|Type::Tiny::Class$ [Prereqs] perl = 5.8.6 Moo = 1.00600 [PkgVersion] [MetaProvides::Package] inherit_version = 1 [PodWeaver] [ReadmeAnyFromPod] [ChangelogFromGit::CPAN::Changes] [Clean] META.yml100644001750001750 215113365151052 16223 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014--- abstract: 'Build Enum-like classes' author: - 'Meredith Howard ' build_requires: File::Find: '0' Test::More: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Class-Type-Enum provides: Class::Type::Enum: file: lib/Class/Type/Enum.pm version: '0.014' DBIx::Class::InflateColumn::ClassTypeEnum: file: lib/DBIx/Class/InflateColumn/ClassTypeEnum.pm version: '0.014' requires: Carp: '0' Class::Method::Modifiers: '0' List::Util: '1.33' Moo: '1.00600' Scalar::Util: '0' namespace::clean: '0' overload: '0' perl: v5.8.6 strict: '0' warnings: '0' resources: bugtracker: https://github.com/merrilymeredith/p5-Class-Type-Enum/issues homepage: http://metacpan.org/release/Class-Type-Enum/ repository: git://github.com/merrilymeredith/p5-Class-Type-Enum.git version: '0.014' x_generated_by_perl: v5.24.1 x_serialization_backend: 'YAML::Tiny version 1.70' MANIFEST100644001750001750 44013365151052 16062 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README README.md dist.ini lib/Class/Type/Enum.pm lib/DBIx/Class/InflateColumn/ClassTypeEnum.pm t/00-load-all.t t/01-basic.t t/02-synopsis.t weaver.ini META.json100644001750001750 356613365151052 16406 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014{ "abstract" : "Build Enum-like classes", "author" : [ "Meredith Howard " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Class-Type-Enum", "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "Class::Method::Modifiers" : "0", "List::Util" : "1.33", "Moo" : "1.00600", "Scalar::Util" : "0", "namespace::clean" : "0", "overload" : "0", "perl" : "v5.8.6", "strict" : "0", "warnings" : "0" } }, "test" : { "requires" : { "File::Find" : "0", "Test::More" : "0" } } }, "provides" : { "Class::Type::Enum" : { "file" : "lib/Class/Type/Enum.pm", "version" : "0.014" }, "DBIx::Class::InflateColumn::ClassTypeEnum" : { "file" : "lib/DBIx/Class/InflateColumn/ClassTypeEnum.pm", "version" : "0.014" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/merrilymeredith/p5-Class-Type-Enum/issues" }, "homepage" : "http://metacpan.org/release/Class-Type-Enum/", "repository" : { "type" : "git", "url" : "git://github.com/merrilymeredith/p5-Class-Type-Enum.git", "web" : "https://github.com/merrilymeredith/p5-Class-Type-Enum" } }, "version" : "0.014", "x_generated_by_perl" : "v5.24.1", "x_serialization_backend" : "Cpanel::JSON::XS version 4.01" } README.md100644001750001750 273713365151052 16243 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014# Class::Type::Enum Class::Type::Enum is a class builder for type-like enumeration classes. It's a bit of an experiment that grew from liking [Object::Enum][objenum] and using it with [DBIC][dbic], but prefering something more akin to defining types, with traditional numeric backing values and sortability. Object::Enum instances are all just instances of Object::Enum. I'd love to hear thoughts or advice. [Object::Enum][objenum] works nicely for varchars with enum-like sets of values, but all enums you get out of it are instances of Object::Enum. Instead, this is a class builder which lets you treat that class as an enum type. Thanks to the ordinal values behind these enums, they can be sorted either by ordinal (`<=>`) or by symbol (`cmp`). This also allows for checks like `$thing->status > $approved` in addition to the usual `$thing->status->is_foo` checks. There is no check that you're comparing similar types, as comparison is just happening through overload fallback after stringify and numify. _Unlike_ Object::Enum, objects are not mutable. I may add methods that return new instances though, for example `next` and `prev`... Also I liked `is_any` and `is_none` from [Enumeration][enumeration] and added the same, as `any` and `none`. ## License This software is licensed under the same terms as the Perl distribution itself. [dbic]: https://metacpan.org/pod/DBIx::Class [objenum]: https://metacpan.org/pod/Object::Enum [enumeration]: https://metacpan.org/pod/Enumeration weaver.ini100644001750001750 5613365151052 16706 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014[@Default] [-Transformer] transformer = List Makefile.PL100644001750001750 267013365151052 16732 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012. use strict; use warnings; use 5.008006; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "Build Enum-like classes", "AUTHOR" => "Meredith Howard ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "Class-Type-Enum", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.008006", "NAME" => "Class::Type::Enum", "PREREQ_PM" => { "Carp" => 0, "Class::Method::Modifiers" => 0, "List::Util" => "1.33", "Moo" => "1.00600", "Scalar::Util" => 0, "namespace::clean" => 0, "overload" => 0, "strict" => 0, "warnings" => 0 }, "TEST_REQUIRES" => { "File::Find" => 0, "Test::More" => 0 }, "VERSION" => "0.014", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Carp" => 0, "Class::Method::Modifiers" => 0, "File::Find" => 0, "List::Util" => "1.33", "Moo" => "1.00600", "Scalar::Util" => 0, "Test::More" => 0, "namespace::clean" => 0, "overload" => 0, "strict" => 0, "warnings" => 0 ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); t000755001750001750 013365151052 15056 5ustar00mhowardmhoward000000000000Class-Type-Enum-0.01401-basic.t100640001750001750 561113365151052 16701 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/tuse warnings; use strict; use Test::More; { package Critter; use Class::Type::Enum values => [qw( mouse rabbit dog cat )]; } { package Vehicle; # Make sure it works more than once use Class::Type::Enum values => [qw(bike car bus train plane)]; } can_ok( 'Critter', qw( new inflate_symbol inflate_ordinal type_constraint test_symbol test_ordinal coerce_symbol coerce_ordinal values sym_to_ord ord_to_sym list_is_methods )); ok( (my @is_methods = Critter->list_is_methods), 'can list_is_methods' ); is( scalar(@is_methods), 4, 'is_methods looks good' ); my $cat = new_ok( 'Critter', ['cat'] ); isa_ok( $cat, 'Class::Type::Enum' ); can_ok( $cat, qw( is is_mouse is_cat is_dog is_rabbit ) ); ok( $cat->is('cat'), 'cat is a cat.'); ok( $cat->is_cat, 'cat is a cat!' ); ok( !$cat->is_dog, 'this aint no dog.' ); is( "$cat", 'cat', "stringified the cat, yeowch!" ); ok( $cat != 1, "are cats even numifiable?" ); ok( $cat == Critter->new('cat'), 'all cats are equal' ); ok( $cat == Critter->new("$cat"), 'no matter where they come from' ); ok( $cat > Critter->new('dog'), '...and more equal than dogs' ); ok( Critter->new('mouse') < $cat, 'these fierce predators' ); ok( Critter->new('mouse') lt $cat, 'no matter how you look at it' ); # Dwarn [ sort {$a <=> $b} map { Critter->new($_) } @{Critter->values} ]; ok( $cat->any(qw(rabbit cat)), 'others could be tolerated' ); ok( $cat->none(qw(dog mouse)), 'but we must keep standards' ); ok( my $dog = $cat->new('dog'), 'okay okay, made a dog' ); cmp_ok( $dog, '!=', $cat, "they're just so different!" ); ok( 'cat' gt $dog, 'cats are still on top though' ); subtest 'test methods for type checks' => sub { ok( Critter->test_symbol('rabbit'), 'rabbit ok' ); ok( Critter->test_symbol($cat), 'cat ok' ); ok( !Critter->test_symbol('snake'), 'no snakes' ); ok( Critter->test_ordinal(0), 'numouse ok' ); ok( Critter->test_ordinal(0+$cat), 'numcat ok'); ok( !Critter->test_ordinal(42), 'life meaningless' ); }; subtest 'coerce methods' => sub { ok( Critter->coerce_symbol($cat), 'coerced cat, maybe' ); ok( Critter->coerce_symbol('rabbit'), 'coerced rabbit' ); ok( !defined eval { Critter->coerce_symbol('snake') }, 'no legs, no service' ); ok( Critter->coerce_ordinal($cat), 'coerced cat, sure buddy' ); ok( Critter->coerce_ordinal(2), 'coerced dog' ); ok( !defined eval { Critter->coerce_ordinal(21) }, 'blackjack' ); ok( Critter->coerce_any($cat), 'coerced cat, sure buddy' ); ok( Critter->coerce_any(2), 'coerced dog' ); ok( Critter->coerce_any('rabbit'), 'coerced bunny' ); ok( !defined eval { Critter->coerce_any('snake') },'cant take a hint' ); ok( !defined eval { Critter->coerce_any(15) }, 'learners permit' ); }; ok( eval { package Dummy; Critter->import(); 1; }, 'Class::Type::Enum skips import when subclasses are used, or this would die.'); done_testing; 00-load-all.t100640001750001750 41513365151052 17261 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/tuse warnings; use strict; use Test::More; use File::Find qw( find ); find { wanted => sub { return unless -f $_ and $_ =~ /.pm$/; my $path = $_; $path =~ s|^lib/||; require_ok($path); warn $@ if $@; }, no_chdir => 1, }, 'lib'; done_testing; 02-synopsis.t100640001750001750 240713365151052 17510 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/tuse warnings; use strict; use Test::More; BEGIN { eval "require Moo; require Type::Tiny;"; plan skip_all => "Synopsis test requires Moo and Type::Tiny" if $@; }; { package Toast::Status; use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt']; } { package Toast; # Don't let this show up as a dependency: BEGIN { Moo->import(); } has status => ( is => 'rw', required => 1, isa => Toast::Status->type_constraint, coerce => 1, handles => [ Toast::Status->list_is_methods ], ); } my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast ); my @trashcan = grep { $_->is_burnt } @toast; my @plate = grep { $_->is_toast } @toast; my $ready_status = Toast::Status->new('toast'); my @eventual_toast = grep { $_->status < $ready_status } @toast; my @eventual_toast_cmp = grep { $_->status lt 'toast' } @toast; is( scalar(@trashcan), 1, "Found one burnt toast" ); is( scalar(@plate), 2, "Found two actual toast" ); is( scalar(@eventual_toast), 3, "And three on the way" ); is( scalar(@eventual_toast_cmp), 3, "Even with string compare" ); eval { Toast->new(status => 'ack') }; ok( index($@, 'Value [ack] is not valid') > 0, 'Type constraint works' ); done_testing; Type000755001750001750 013365151052 17347 5ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/lib/ClassEnum.pm100640001750001750 2122513365151052 20767 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/lib/Class/Typepackage Class::Type::Enum; # ABSTRACT: Build Enum-like classes $Class::Type::Enum::VERSION = '0.014'; use strict; use warnings; use Carp qw(croak); use Class::Method::Modifiers qw(install_modifier); use List::Util 1.33; use Scalar::Util qw(blessed); use namespace::clean; use overload ( '""' => 'stringify', 'cmp' => 'cmp', '0+' => 'numify', fallback => 1, ); sub import { my ($class, %params) = @_; # import is inherited, but we don't want to do all this to everything that # uses a subclass of Class::Type::Enum. return unless $class eq __PACKAGE__; # If there's a use case for it, we can still allow extending CTE subclasses. my $target = caller; my %values; if (ref $params{values} eq 'ARRAY') { my $i = 0; %values = map { $_ => $i++ } @{$params{values}}; } elsif (ref $params{values} eq 'HASH') { %values = %{$params{values}}; } else { croak "Enum values must be provided either as an array or hash ref."; } ## the bits that are installed into the target class, plus @ISA { no strict 'refs'; push @{"${target}::ISA"}, $class; } install_modifier $target, 'fresh', sym_to_ord => sub { \%values }; install_modifier $target, 'fresh', ord_to_sym => sub { +{ reverse(%values) } }; install_modifier $target, 'fresh', values => sub { my $ord = $_[0]->sym_to_ord; [ sort { $ord->{$a} <=> $ord->{$b} } keys %values ]; }; for my $value (keys %values) { install_modifier $target, 'fresh', "is_$value" => sub { $_[0]->is($value) }; } } sub new { my ($class, $value) = @_; (blessed($class) || $class)->inflate_symbol($value); } sub inflate_symbol { my ($class, $symbol) = @_; my $ord = $class->sym_to_ord->{$symbol}; croak "Value [$symbol] is not valid for enum $class" unless defined $ord; bless \$ord, $class; } sub inflate_ordinal { my ($class, $ord) = @_; croak "Ordinal [$ord] is not valid for enum $class" unless exists $class->ord_to_sym->{$ord}; bless \$ord, $class; } sub list_is_methods { my ($class) = @_; map "is_$_", @{$class->values}; } sub type_constraint { my ($class) = @_; require Type::Tiny::Class; require Types::Standard; Type::Tiny::Class->new(class => blessed($class) || $class) ->plus_constructors(Types::Standard::Str(), 'inflate_symbol'); } sub test_symbol { my ($class, $value) = @_; exists($class->sym_to_ord->{$value}) } sub test_ordinal { my ($class, $value) = @_; exists($class->ord_to_sym->{$value}) } sub coerce_symbol { my ($class, $value) = @_; return $value if eval { $value->isa($class) }; $class->inflate_symbol($value); } sub coerce_ordinal { my ($class, $value) = @_; return $value if eval { $value->isa($class) }; $class->inflate_ordinal($value); } sub coerce_any { my ($class, $value) = @_; return $value if eval { $value->isa($class) }; for my $method (qw( inflate_ordinal inflate_symbol )) { my $enum = eval { $class->$method($value) }; return $enum if $enum; } croak "Could not coerce invalid value [$value] into $class"; } sub is { my ($self, $value) = @_; my $ord = $self->sym_to_ord->{$value}; croak "Value [$value] is not valid for enum " . blessed($self) unless defined $ord; $$self == $ord; } sub stringify { my ($self) = @_; $self->ord_to_sym->{$$self}; } sub numify { my ($self) = @_; $$self; } sub cmp { my ($self, $other, $reversed) = @_; return -1 * $self->cmp($other) if $reversed; return $$self <=> $other if blessed($other); my $ord = $self->sym_to_ord->{$other}; croak "Cannot compare to invalid symbol [$other] for " . blessed($self) unless defined $ord; return $$self <=> $ord; } sub any { my ($self, @cases) = @_; List::Util::any { $self->is($_) } @cases; } sub none { my ($self, @cases) = @_; List::Util::none { $self->is($_) } @cases; } 1; __END__ =pod =encoding UTF-8 =head1 NAME Class::Type::Enum - Build Enum-like classes =head1 VERSION version 0.014 =head1 SYNOPSIS package Toast::Status { use Class::Type::Enum values => ['bread', 'toasting', 'toast', 'burnt']; } package Toast { use Moo; has status => ( is => 'rw', isa => Toast::Status->type_constraint, coerce => 1, handles => [ Toast::Status->list_is_methods ], ); } my @toast = map { Toast->new(status => $_) } qw( toast burnt bread bread toasting toast ); my @trashcan = grep { $_->is_burnt } @toast; my @plate = grep { $_->is_toast } @toast; my $ready_status = Toast::Status->new('toast'); my @eventual_toast = grep { $_->status < $ready_status } @toast; # or: @eventual_toast = grep { $_->status lt 'toast' } @toast; # or: @eventual_toast = grep { $_->status->none('toast', 'burnt') } @toast; =head1 DESCRIPTION Class::Type::Enum is a class builder for type-like classes to represent your enumerated values. In particular, it was built to scratch an itch with L value inflation. I wouldn't consider the interface stable yet; I'd love feedback on this dist. When Cing Class::Type::Enum: =over 4 =item * Required: =over 4 =item values => [@symbols] The list of symbolic values in your enum, in ascending order if relevant. =item values => {symbol => ordinal, ...} The list of symbols and ordinal values in your enum. There is no check that a given ordinal isn't reused. =back =back =head2 Custom Ordinal Values If you'd like to build an enum that works like a bitfield or some other custom setup, you need only pass a more explicit hashref to Class::Type::Enum. package BitField { use Class::Type::Enum values => { READ => 1, WRITE => 2, EXECUTE => 4, }; } =head1 METHODS =head2 $class->import(values => ...) Sets up the consuming class as a subclass of Class::Type::Enum and installs functions that are unique to the class. =head2 $class->new($value) Your basic constructor, expects only a value corresponding to a symbol in the enum type. Also works as an instance method for enums of the same class. =head2 $class->inflate_symbol($symbol) Does the actual work of C<$class-Enew($value)>, also used when inflating values for L. =head2 $class->inflate_ordinal($ord) Used when inflating ordinal values for L or if you need to work with ordinals directly. =head2 $class->sym_to_ord Returns a hashref keyed by symbol, with ordinals as values. =head2 $class->ord_to_sym Returns a hashref keyed by ordinal, with symbols as values. =head2 $class->values Returns an arrayref of valid symbolic values, in order. =head2 $class->list_is_methods Returns a list of C methods defined for each symbolic value for the class. =head2 $class->type_constraint This method requires the optional dependency L. Returns a type constraint suitable for use with L and friends. =head2 $class->test_symbol($value) Test whether or not the given value is a valid symbol in this enum class. =head2 $class->test_ordinal($value) Test whether or not the given value is a valid ordinal in this enum class. =head2 $class->coerce_symbol($value) If the given value is already a $class, return it, otherwise try to inflate it as a symbol. Dies on invalid value. =head2 $class->coerce_ordinal($value) If the given value is already a $class, return it, otherwise try to inflate it as an ordinal. Dies on invalid value. =head2 $class->coerce_any($value) If the given value is already a $class, return it, otherwise try to inflate it first as an ordinal, then as a symbol. Dies on invalid value. =head2 $o->is($value) Given a test symbol, test that the enum instance's value is equivalent. An exception is thrown if an invalid symbol is provided =head2 $o->is_$value Shortcut for C<$o-Eis($value)> =head2 $o->stringify Returns the symbolic value. =head2 $o->numify Returns the ordinal value. =head2 $o->cmp($other, $reversed = undef) The string-compare implementation used by overloading. Returns the same values as C. The optional third argument is an artifact of L, set to true if the order of C<$o> and C<$other> have been reversed in order to make the overloaded method call work. =head2 $o->any(@cases) True if C<$o-Eis(..)> for any of the given cases. =head2 $o->none(@cases) True if C<$o-Eis(..)> for none of the given cases. =head1 SEE ALSO =over 4 =item * L =item * L =item * L =back =head1 AUTHOR Meredith Howard =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2018 by Meredith Howard. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut InflateColumn000755001750001750 013365151052 21754 5ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/lib/DBIx/ClassClassTypeEnum.pm100640001750001750 617213365151052 25210 0ustar00mhowardmhoward000000000000Class-Type-Enum-0.014/lib/DBIx/Class/InflateColumnpackage DBIx::Class::InflateColumn::ClassTypeEnum; # ABSTRACT: Inflate enum-like columns to your Class::Type::Enum classes $DBIx::Class::InflateColumn::ClassTypeEnum::VERSION = '0.014'; use warnings; use strict; use Carp (); sub register_column { my ($self, $column, $info) = @_; $self->next::method($column, $info); return unless $info->{extra} and my $class = $info->{extra}{enum_class}; unless (eval { $class->isa('Class::Type::Enum') }) { Carp::croak "enum_class $class is not loaded or doesn't inherit from Class::Type::Enum"; } # I'd love to DTRT based on the column type but I think they're practically # freeform in DBIC and just match the DB types, so that's a lot of # possibilities... if ($info->{extra}{enum_ordinal_storage}) { $self->inflate_column( $column => { inflate => sub { my ($ord) = @_; return unless defined $ord; $class->inflate_ordinal($ord); }, deflate => sub { my ($enum) = @_; return unless defined $enum; $enum->numify; }, } ); } else { $self->inflate_column( $column => { inflate => sub { my ($val) = @_; return unless defined $val; $class->inflate_symbol($val); }, deflate => sub { my ($enum) = @_; return unless defined $enum; $enum->stringify; }, } ); } } 1; __END__ =pod =encoding UTF-8 =head1 NAME DBIx::Class::InflateColumn::ClassTypeEnum - Inflate enum-like columns to your Class::Type::Enum classes =head1 VERSION version 0.014 =head1 SYNOPSIS package My::Schema::Result::Toast { __PACKAGE__->load_components(qw/ InflateColumn::ClassTypeEnum Core /); # Assuming Toast::Status is one of your enum classes... use Toast::Status; # Ensure it is loaded. __PACKAGE__->add_columns( status => { data_type => 'varchar', extra => { enum_class => 'Toast::Status', }, } ); } =head1 DESCRIPTION Inflate DBIC columns into instances of your L classes. The storage C doesn't matter here, only whether or not enums should inflate/deflate to symbols (strings) or ordinals (integers). =head1 METHODS =head2 register_column($column, $info) This method chains with L and checks for two subkeys inside the C key of the column info: =over 4 =item enum_class Required to enable column inflation. Specify the complete class name that this column should be inflated to. It should already be loaded and must be a subclass of L. =item enum_ordinal_storage If true, the column is inflated from and deflated to ordinal values. =back =head1 SEE ALSO =over 4 =item * L =item * L =back =head1 AUTHOR Meredith Howard =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2018 by Meredith Howard. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut