File Coverage

blib/lib/Moonshine/Magic.pm
Criterion Covered Total %
statement 38 81 46.9
branch 0 20 0.0
condition 0 2 0.0
subroutine 12 16 75.0
pod n/a
total 50 119 42.0


line stmt bran cond sub pod time code
1             package Moonshine::Magic;
2              
3 1     1   14808 use strict;
  1         2  
  1         25  
4 1     1   3 use warnings;
  1         1  
  1         28  
5              
6 1     1   402 use BEGIN::Lift;
  1         5166  
  1         24  
7 1     1   435 use Moonshine::Element;
  1         28198  
  1         39  
8 1     1   508 use Moonshine::Util "assert_valid_html5_tag" => { -as => "assert_valid_tag" };
  1         17194  
  1         14  
9              
10 1     1   740 use MOP::Class;
  1         16512  
  1         43  
11 1     1   9 use MOP::Role;
  1         1  
  1         25  
12 1     1   4 use Carp qw/croak/;
  1         1  
  1         122  
13              
14             =head1 NAME
15              
16             Moonshine::Magic - d[ o_0 ]b - has, extends, lazy_components
17              
18             =head1 VERSION
19              
20             Version 0.07
21              
22             =cut
23              
24             our $VERSION = '0.07';
25              
26             =head1 SYNOPSIS
27              
28             use Moonshine::Magic;
29              
30             =head1 Imports
31              
32             =head2 extends
33              
34             extends 'Moonshine::Component';
35             # BEGIN { @ISA = ('Moonshine::Component') }
36              
37             =head2 has
38              
39             has (
40             'modify_spec' => sub { { switch => 0, switch_base => 0 } },
41             );
42              
43             # BEGIN { @HAS = ( 'modify_spec' => sub { { switch => 0, switch_base => 0 } } }
44             # $self->modify_spec
45              
46             =head2 with
47              
48             with 'Moonshine::Component::Glyphicon';
49              
50             =head2 lazy_components
51              
52             lazy_components (qw/p span h1/);
53             # $self->span() -
54              
55             =cut
56              
57             sub import {
58 1     1   10 my $caller = caller;
59              
60             BEGIN::Lift::install(
61             ( $caller, 'extends' ) => sub {
62 1     1   5 no strict 'refs';
  1         1  
  1         169  
63 0     0   0 my @extends = @_;
64              
65             unshift @extends, 'UNIVERSAL::Object'
66 0 0       0 unless grep { $_ eq 'UNIVERSAL::Object' } @extends;
  0         0  
67              
68 0         0 for (@extends) {
69 0         0 eval "require $_";
70 0 0       0 croak $@ if $@;
71             }
72              
73 0         0 @{"${caller}::ISA"} = @extends;
  0         0  
74             }
75 1         7 );
76              
77             BEGIN::Lift::install(
78             ( $caller, 'has' ) => sub {
79 0     0   0 my %args = @_;
80 1     1   6 no strict 'refs';
  1         1  
  1         45  
81 1     1   6 no warnings 'once';
  1         2  
  1         654  
82 0         0 %{"${caller}::HAS"} = %args;
  0         0  
83              
84 0         0 my $class = MOP::Class->new($caller);
85 0         0 for my $arg ( keys %args ) {
86 0         0 $class->add_method( $arg, sub { return $args{$arg}->(); } );
  0         0  
87             }
88             }
89 1         104 );
90              
91             BEGIN::Lift::install(
92             ( $caller, 'with' ) => sub {
93 0     0   0 my @roles = @_;
94 0 0       0 croak "No roles supplied!" unless @roles;
95 0         0 my $class = MOP::Class->new($caller);
96 0         0 for my $r (@roles) {
97 0         0 eval "require $r";
98 0 0       0 croak $@ if $@;
99              
100 0         0 my $role = MOP::Class->new($r);
101 0         0 for my $meth ( $role->all_methods ) {
102 0 0       0 next if $meth->name eq '__ANON__';
103 0         0 $class->add_method( $meth->name, $meth->body );
104             }
105              
106 0         0 for my $slot ( $role->all_slots ) {
107 0 0       0 next if $class->has_slot($slot->name);
108 0         0 $class->alias_slot( $slot->name, $slot->initializer );
109 0 0       0 next if $class->has_method($slot->name);
110 0         0 $class->add_method( $slot->name, $slot->initializer );
111             }
112             }
113             }
114 1         65 );
115              
116             BEGIN::Lift::install(
117             ( $caller, 'lazy_components' ) => sub {
118 0     0     my @lazy_components = @_;
119 0           my $class = MOP::Class->new($caller);
120 0           for my $component (@lazy_components) {
121 0 0         next unless assert_valid_tag($component);
122             $class->add_method(
123             $component,
124             sub {
125 0           my $self = shift;
126              
127 0           my ( $base_args, $build_args ) = ();
128 0 0         if ( $self->can('validate_build') ) {
129             ( $base_args, $build_args ) = $self->validate_build(
130             {
131             params => $_[0] // {},
132             spec => {
133             tag => { default => $component },
134             data => 0,
135 0 0 0       ( $_[1] ? %{ $_[1] } : () )
  0            
136             }
137             }
138             );
139             }
140             else {
141 0           $base_args = { tag => $component, %{ $_[0] } };
  0            
142             }
143              
144 0           return Moonshine::Element->new($base_args);
145             }
146 0           );
147             }
148             }
149 1         82 );
150             }
151              
152             =head1 AUTHOR
153              
154             Robert Acock, C<< >>
155              
156             =head1 BUGS
157              
158             Please report any bugs or feature requests to C, or through
159             the web interface at L. I will be notified, and then you'll
160             automatically be notified of progress on your bug as I make changes.
161              
162             =head1 SUPPORT
163              
164             You can find documentation for this module with the perldoc command.
165              
166             perldoc Moonshine::Magic
167              
168             You can also look for information at:
169              
170             =over 4
171              
172             =item * RT: CPAN's request tracker (report bugs here)
173              
174             L
175              
176             =item * AnnoCPAN: Annotated CPAN documentation
177              
178             L
179              
180             =item * CPAN Ratings
181              
182             L
183              
184             =item * Search CPAN
185              
186             L
187              
188             =back
189              
190              
191             =head1 ACKNOWLEDGEMENTS
192              
193              
194             =head1 LICENSE AND COPYRIGHT
195              
196             Copyright 2017 Robert Acock.
197              
198             This program is free software; you can redistribute it and/or modify it
199             under the terms of the the Artistic License (2.0). You may obtain a
200             copy of the full license at:
201              
202             L
203              
204             Any use, modification, and distribution of the Standard or Modified
205             Versions is governed by this Artistic License. By using, modifying or
206             distributing the Package, you accept this license. Do not use, modify,
207             or distribute the Package, if you do not accept this license.
208              
209             If your Modified Version has been derived from a Modified Version made
210             by someone other than you, you are nevertheless required to ensure that
211             your Modified Version complies with the requirements of this license.
212              
213             This license does not grant you the right to use any trademark, service
214             mark, tradename, or logo of the Copyright Holder.
215              
216             This license includes the non-exclusive, worldwide, free-of-charge
217             patent license to make, have made, use, offer to sell, sell, import and
218             otherwise transfer the Package with respect to any patent claims
219             licensable by the Copyright Holder that are necessarily infringed by the
220             Package. If you institute patent litigation (including a cross-claim or
221             counterclaim) against any party alleging that the Package constitutes
222             direct or contributory patent infringement, then this Artistic License
223             to you shall terminate on the date that such litigation is filed.
224              
225             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
226             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
227             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
228             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
229             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
230             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
231             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
232             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
233              
234              
235             =cut
236              
237             1; # End of Moonshine::Magic