File: ClassTypeEnum.pm

package info (click to toggle)
libclass-type-enum-perl 0.014-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: perl: 178; makefile: 2
file content (149 lines) | stat: -rw-r--r-- 3,194 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package 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<Class::Type::Enum> classes.  The
storage C<data_type> 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<DBIx::Class::Row/register_column> and checks for two
subkeys inside the C<extra> 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<Class::Type::Enum>.

=item enum_ordinal_storage

If true, the column is inflated from and deflated to ordinal values.

=back

=head1 SEE ALSO

=over 4

=item *

L<Class::Type::Enum>

=item *

L<DBIx::Class::InflateColumn::Object::Enum>

=back

=head1 AUTHOR

Meredith Howard <mhoward@cpan.org>

=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