File Coverage

blib/lib/YATT/Lite/VarMaker.pm
Criterion Covered Total %
statement 40 40 100.0
branch 6 6 100.0
condition 10 11 90.9
subroutine 12 12 100.0
pod 1 5 20.0
total 69 74 93.2


line stmt bran cond sub pod time code
1             package YATT::Lite::VarMaker; sub MY () {__PACKAGE__}
2 17     17   138 use strict;
  17         36  
  17         518  
3 17     17   85 use warnings qw(FATAL all NONFATAL misc);
  17         41  
  17         562  
4 17     17   92 use base qw(YATT::Lite::Object);
  17         47  
  17         4931  
5 17         103 use YATT::Lite::MFields qw/type_alias
6 17     17   4430 curline/;
  17         44  
7              
8 17     17   7880 use YATT::Lite::VarTypes qw(:type :VSLOT);
  17         47  
  17         3277  
9 17     17   119 use YATT::Lite::Util qw(lexpand default);
  17         38  
  17         6505  
10              
11             # XXX: Should integrated to VarTypes.
12 186     186 0 631 sub default_arg_type {'text'}
13             sub default_type_alias {
14 667     667 0 3567 qw(value scalar
15             flag bool
16             boolean bool
17             expr code);
18             }
19              
20             sub after_new {
21 667     667 1 1184 my MY $self = shift;
22 667         2431 $self->SUPER::after_new;
23 667         1869 $self->{type_alias} = { $self->default_type_alias };
24 667         1728 $self;
25             }
26              
27             # Note: To use mkvar_at(), derived class must implement
28             # _error() and _tmpl_file_line().
29             # Currently, YATT::Lite::LRXML and YATT::Lite::CGen uses this.
30             sub mkvar_at {
31 1734     1734 0 4539 (my MY $self, my ($lineno, $type, $name, @args)) = @_;
32              
33 1734         4147 my ($typerec, $sub) = $self->_mk_typerec($type, $lineno, $name);
34              
35 1733         8805 my $var = $sub->()->new($typerec, $name, @args);
36 1733   100     10850 $var->[VSLOT_LINENO] //= $lineno //= $self->{curline};
      100        
37 1733         6544 $var;
38             }
39              
40             sub set_var_type {
41 1     1 0 16 (my MY $self, my ($var, $type)) = @_;
42 1         3 $var->type(scalar $self->_mk_typerec($type));
43             }
44              
45             sub _mk_typerec {
46 1735     1735   3531 (my MY $self, my ($type, $lineno, $name)) = @_;
47              
48 1735 100 100     6702 ($type, my @subtype) = ref $type ? lexpand($type) : split /:/, $type || '';
49             #
50 1735   66     4568 $type ||= $self->default_arg_type;
51 1735         6185 $type = default($self->{type_alias}{$type}, $type);
52              
53 1735         4469 my $typerec = [$type, @subtype];
54              
55 1735 100       3781 wantarray ? ($typerec, do {
56 1734 100       9613 my $sub = $self->can("t_$type") or do {
57 1         7 my %opts = ($self->_tmpl_file_line($lineno));
58 1         6 die $self->_error(\%opts, q|Unknown type '%s' for variable '%s'|
59             , $type, $name);
60             };
61             }) : $typerec;
62             }
63              
64             1;