File Coverage

blib/lib/Slack/BlockKit/Role/HasStyle.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Slack::BlockKit::Role::HasStyle 0.005;
2             # ABSTRACT: a parameterized role for objects with styles
3              
4 4     4   5337 use MooseX::Role::Parameterized;
  4         299863  
  4         19  
5              
6             #pod =head1 OVERVIEW
7             #pod
8             #pod This role exists to help write classes for Block Kit objects that have text
9             #pod styles applied. Because not all objects with styles permit all the same
10             #pod styles, this is a I<parameterized> role, and must be included by providing a
11             #pod C<styles> parameter, which is an arrayref of style names that may be enabled or
12             #pod disabled on an object.
13             #pod
14             #pod When a Block Kit object class that composes this role is converted into a data
15             #pod structure with C<as_struct>, the styled defined in that instance's C<style>
16             #pod hash will be added as JSON boolean objects.
17             #pod
18             #pod You probably don't need to think about this role, though.
19             #pod
20             #pod =cut
21              
22 4     4   191925 use v5.36.0;
  4         17  
23              
24 4     4   28 use MooseX::Types::Moose qw(ArrayRef Bool);
  4         9  
  4         53  
25 4     4   34198 use MooseX::Types::Structured qw(Dict Optional);
  4         9  
  4         43  
26              
27             my sub _boolset ($hashref) {
28             return {
29             map {; $_ => Slack::BlockKit::boolify($hashref->{$_}) } keys %$hashref,
30             };
31             }
32              
33             parameter styles => (
34             is => 'bare',
35             isa => 'ArrayRef[Str]',
36             required => 1,
37             traits => [ 'Array' ],
38             handles => { styles => 'elements' },
39             );
40              
41             role {
42             my ($param) = @_;
43              
44             has style => (
45             is => 'ro',
46             isa => Dict[ map {; $_ => Optional([Bool]) } $param->styles ],
47             predicate => 'has_style',
48             );
49              
50             around as_struct => sub {
51             my ($orig, $self, @rest) = @_;
52              
53             my $struct = $self->$orig(@rest);
54              
55             if ($self->has_style) {
56             $struct->{style} = _boolset($self->style);
57             }
58              
59             return $struct;
60             };
61             };
62              
63 4     4   2263 no MooseX::Types::Moose;
  4         8  
  4         144  
64 4     4   18 no MooseX::Types::Structured;
  4         7  
  4         109  
65              
66 4     4   24 no Moose::Role;
  4         8  
  4         26  
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Slack::BlockKit::Role::HasStyle - a parameterized role for objects with styles
78              
79             =head1 VERSION
80              
81             version 0.005
82              
83             =head1 PERL VERSION
84              
85             This module should work on any version of perl still receiving updates from
86             the Perl 5 Porters. This means it should work on any version of perl
87             released in the last two to three years. (That is, if the most recently
88             released version is v5.40, then this module should work on both v5.40 and
89             v5.38.)
90              
91             Although it may work on older versions of perl, no guarantee is made that the
92             minimum required version will not be increased. The version may be increased
93             for any reason, and there is no promise that patches will be accepted to
94             lower the minimum required perl.
95              
96             =head1 OVERVIEW
97              
98             This role exists to help write classes for Block Kit objects that have text
99             styles applied. Because not all objects with styles permit all the same
100             styles, this is a I<parameterized> role, and must be included by providing a
101             C<styles> parameter, which is an arrayref of style names that may be enabled or
102             disabled on an object.
103              
104             When a Block Kit object class that composes this role is converted into a data
105             structure with C<as_struct>, the styled defined in that instance's C<style>
106             hash will be added as JSON boolean objects.
107              
108             You probably don't need to think about this role, though.
109              
110             =head1 AUTHOR
111              
112             Ricardo SIGNES <cpan@semiotic.systems>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2024 by Ricardo SIGNES.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut