Gearman::Driver::Worker::AttributeParser - Parses worker attributes


Gearman-Driver documentation Contained in the Gearman-Driver distribution.

Index


Code Index:

NAME

Top

Gearman::Driver::Worker::AttributeParser - Parses worker attributes

DESCRIPTION

Top

This module is responsible for parsing the method attributes of a worker. It has no public interface currently.

AUTHOR

Top

See Gearman::Driver.

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

* Gearman::Driver
* Gearman::Driver::Adaptor
* Gearman::Driver::Console
* Gearman::Driver::Console::Basic
* Gearman::Driver::Console::Client
* Gearman::Driver::Job
* Gearman::Driver::Job::Method
* Gearman::Driver::Loader
* Gearman::Driver::Observer
* Gearman::Driver::Worker
* Gearman::Driver::Worker::Base

Gearman-Driver documentation Contained in the Gearman-Driver distribution.
package Gearman::Driver::Worker::AttributeParser;

use Moose::Role;

has 'parsed_attributes' => (
    builder => '_parse_attributes',
    handles => {
        has_attribute => 'defined',
        get_attribute => 'get',
    },
    is     => 'ro',
    isa    => 'HashRef',
    lazy   => 1,
    traits => [qw(Hash)],
);

has 'default_attributes' => (
    default => sub { {} },
    is      => 'rw',
    isa     => 'HashRef',
);

has 'override_attributes' => (
    default => sub { {} },
    is      => 'rw',
    isa     => 'HashRef',
);

has 'valid_attributes' => (
    auto_deref => 1,
    default    => sub {
        [
            qw(
              Encode
              Decode
              Job
              MaxProcesses
              MinProcesses
              ProcessGroup
              )
        ];
    },
    is  => 'rw',
    isa => 'ArrayRef',
);

sub _parse_attributes {
    my ($self) = @_;

    my $attributes = $self->attributes;

    my $result = {};

    foreach my $attr ( keys %{ $self->default_attributes } ) {
        unshift @$attributes, sprintf '%s(%s)', $attr, $self->default_attributes->{$attr};
    }

    foreach my $attr ( keys %{ $self->override_attributes } ) {
        push @$attributes, sprintf '%s(%s)', $attr, $self->override_attributes->{$attr};
    }

    foreach my $attr (@$attributes) {
        my ( $type, $value ) = $attr =~ / (\w+) (?: \( (.*?) \) )*/x;

        # Default values
        $value ||= 'encode'    if $type eq 'Encode';
        $value ||= 'decode'    if $type eq 'Decode';
        $value ||= $self->name if $type eq 'ProcessGroup';
        $value = 1 unless defined $value;

        unless ( grep $type eq $_, $self->valid_attributes ) {
            warn "Invalid attribute '$attr' in " . ref($self);
            next;
        }

        $result->{$type} = $value if defined $value;
    }

    return $result;
}

no Moose::Role;

1;