Markdent::Role::BalancedEvent - A parameterized role for events which can check if they match balance another event


Markdent documentation Contained in the Markdent distribution.

Index


Code Index:

NAME

Top

Markdent::Role::BalancedEvent - A parameterized role for events which can check if they match balance another event

VERSION

Top

version 0.17

DESCRIPTION

Top

This role provides behavior for start and end events which can be checked for a balancing event. This includes things like strong, emphasis, and code start/end events.

ROLE PARAMETERS

Top

This role accepts the following parameters:

* compare => [ ... ]

This should be a list of attribute names which will be compared between the start and end events.

METHODS

Top

This role provides the following methods:

$event->balances_event($event2)

Given an event, this returns true if two events balance each other. This is done by comparing types (StartCode matches EndCode), as well as the attributes provided in the compare parameter.

BUGS

Top

See Markdent for bug reporting details.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Markdent documentation Contained in the Markdent distribution.

package Markdent::Role::BalancedEvent;
BEGIN {
  $Markdent::Role::BalancedEvent::VERSION = '0.17';
}

use strict;
use warnings;

use List::AllUtils qw( all );
use Markdent::Types qw( ArrayRef Str );
use MooseX::Params::Validate qw( pos_validated_list );

use namespace::autoclean;
use MooseX::Role::Parameterized;

parameter compare => (
    isa => ArrayRef[Str],
);

role {
    my $p = shift;

    my @compare = @{ $p->compare() || [] };

    method balances_event => sub {
        my $self = shift;
        my ($other) = pos_validated_list( \@_, { does => 'Markdent::Role::Event' } );

        return 0 unless $self->name() eq $other->name();

        return 0
            unless ( $self->is_start() && $other->is_end() )
            || ( $self->is_end() && $other->is_start() );

        return 1 unless @compare;

        return all { $self->$_() eq $other->$_() } @compare;

        return 0;
    };
};

1;

# ABSTRACT: A parameterized role for events which can check if they match balance another event




__END__