Solution

 view release on metacpan or  search on metacpan

lib/Solution/Block.pm  view on Meta::CPAN

package Solution::Block;
{
    use strict;
    use warnings;
    our $VERSION = '0.9.1';
    use lib '../../lib';
    use Solution::Error;
    our @ISA = qw[Solution::Document];

    sub new {
        my ($class, $args) = @_;
        raise Solution::ContextError {message => 'Missing template argument',
                                      fatal   => 1
            }
            if !defined $args->{'template'};
        raise Solution::ContextError {message => 'Missing parent argument',
                                      fatal   => 1
            }
            if !defined $args->{'parent'};
        raise Solution::SyntaxError {
             message => 'else tags are non-conditional: ' . $args->{'markup'},
             fatal   => 1
            }
            if $args->{'tag_name'} eq 'else' && $args->{'attrs'};
        my $self = bless {tag_name   => 'b-' . $args->{'tag_name'},
                          conditions => undef,
                          nodelist   => [],
                          template   => $args->{'template'},
                          parent     => $args->{'parent'},
        }, $class;
        $self->{'conditions'} = (
            $args->{'tag_name'} eq 'else' ?
                [1]
            : sub {    # Oh, what a mess...
                my @conditions
                    = split m[\s+\b(and|or)\b\s+],
                    $args->{parent}->{tag_name} eq 'for' ?
                    1
                    : (defined $args->{'attrs'} ? $args->{'attrs'} : '');
                my @equality;
                while (my $x = shift @conditions) {
                    push @equality, (
                        $x =~ m[\b(?:and|or)\b]    # XXX - ARG
                        ?
                            bless({template  => $args->{'template'},
                                   parent    => $self,
                                   condition => $x,
                                   lvalue    => pop @equality,
                                   rvalue =>
                                       Solution::Condition->new(
                                          {template => $args->{'template'},
                                           parent   => $self,
                                           attrs    => shift @conditions
                                          }
                                       )
                                  },
                                  'Solution::Condition'
                            )
                        : Solution::Condition->new(
                                          {attrs    => $x,
                                           template => $args->{'template'},
                                           parent   => $self,
                                          }
                        )
                    );
                }
                \@equality;
                }
                ->()
        );
        return $self;
    }
}
1;

=pod

=head1 NAME

Solution::Block - Simple Node Type

=head1 Description

There's not really a lot to say about basic blocks. The real action is in the
classes which make use of them or subclass it. See L<if|Solution::Tag::If>,
L<unless|Solution::Tag::Unless>, or L<case|Solution::Tag::Case>.

=head1 Bugs

Liquid's (and by extension L<Solution|Solution>'s) treatment of
compound inequalities is broken. For example...

    {% if 'This and that' contains 'that' and 1 == 3 %}

...would be parsed as if it were...



( run in 1.352 second using v1.01-cache-2.11-cpan-71847e10f99 )