File Coverage

blib/lib/Form/Tiny/FieldDefinitionBuilder.pm
Criterion Covered Total %
statement 40 41 97.5
branch 8 10 80.0
condition 5 6 83.3
subroutine 10 10 100.0
pod 0 1 0.0
total 63 68 92.6


line stmt bran cond sub pod time code
1             package Form::Tiny::FieldDefinitionBuilder;
2             $Form::Tiny::FieldDefinitionBuilder::VERSION = '2.26';
3 53     53   791 use v5.10;
  53         206  
4 53     53   326 use strict;
  53         119  
  53         1594  
5 53     53   283 use warnings;
  53         296  
  53         2733  
6 53     53   308 use Moo;
  53         131  
  53         414  
7 53     53   23121 use Carp qw(croak);
  53         161  
  53         3610  
8 53     53   361 use Scalar::Util qw(blessed);
  53         130  
  53         2986  
9 53     53   320 use Types::Standard qw(HashRef);
  53         157  
  53         1008  
10              
11 53     53   111903 use Form::Tiny::FieldDefinition;
  53         303  
  53         2929  
12 53     53   515 use Form::Tiny::Utils qw(has_form_meta);
  53         120  
  53         20142  
13              
14             has 'build_data' => (
15             is => 'ro',
16             required => 1,
17             );
18              
19             has 'addons' => (
20             is => 'ro',
21             isa => HashRef,
22             default => sub { {} },
23             init_arg => undef,
24             );
25              
26             sub build
27             {
28 306     306 0 9049 my ($self, $context) = @_;
29              
30 306         1044 my $data = $self->build_data;
31 306         1301 my $dynamic = ref $data eq 'CODE';
32 306 100 100     1230 if ($dynamic && defined blessed $context) {
33 8 50       39 croak 'building a dynamic field definition requires Form::Tiny form'
34             unless has_form_meta($context);
35 8         74 $data = $data->($context);
36 8         1300 $dynamic = 0;
37             }
38              
39 306 100       780 return $self if $dynamic;
40              
41 300         559 my $definition;
42 300 100 66     1645 if (defined blessed $data && $data->isa('Form::Tiny::FieldDefinition')) {
    50          
43 2         5 $definition = $data;
44             }
45             elsif (ref $data eq 'HASH') {
46 298         6937 $definition = Form::Tiny::FieldDefinition->new($data);
47             }
48             else {
49 0         0 croak sprintf q{Invalid form field '%s' data: must be hashref or instance of Form::Tiny::FieldDefinition},
50             $self->name;
51             }
52              
53 293         8045 $definition->set_addons($self->addons);
54              
55 293         8659 return $definition;
56             }
57              
58             1;
59