CGI::Application::Plugin::ActionDispatch::Attributes - Hidden attribute support for CGI::Application


CGI-Application-Plugin-ActionDispatch documentation Contained in the CGI-Application-Plugin-ActionDispatch distribution.

Index


Code Index:

NAME

Top

CGI::Application::Plugin::ActionDispatch::Attributes - Hidden attribute support for CGI::Application

SYNOPSIS

Top

  use CGI::Application::Plugin::ActionDispatch::Attributes;

  sub CGI::Application::Protected : ATTR {
	my( $package, $referent, $attr, $data ) = @_;
	...
  }
  CGI::Application::Plugin::ActionDispatch::Attributes::init();

  sub my_method Protected {
	...
  }

DESCRIPTION

Top

This module will add attribute support into CGI::Application. It will also not break mod_perl.

T

SEE ALSO

Top

AUTHOR

Top

Jason Yates, <jaywhy@gmail.com>

COPYRIGHT AND LICENSE

Top


CGI-Application-Plugin-ActionDispatch documentation Contained in the CGI-Application-Plugin-ActionDispatch distribution.

package CGI::Application::Plugin::ActionDispatch::Attributes;

use attributes;
use strict;
use Data::Dumper;

our $VERSION = '0.1';

my @attributes;
my %attr_handlers;

my $init = 1;

# MODIFY_CODE_ATTRIBUTES needs to be in the inheritance tree.
push @CGI::Application::ISA, 'CGI::Application::Plugin::ActionDispatch::Attributes'
	unless grep /^CGI::Application::Plugin::ActionDispatch::Attributes$/, @CGI::Application::ISA;

sub MODIFY_CODE_ATTRIBUTES {
	my($class, $code, @attrs) = @_;

	foreach (@attrs) {
		# Parse the attribute string ex: Regex('^/foo/bar/(\d+)/').
		my($method, $params) = /^([a-z_]\w*)(?:[(](.*)[)])?$/is;
		$params =~ s/(^'|'$)//g if defined $params;

		# Attribute definition.
		if($method eq 'ATTR') {
			$attr_handlers{$code} = $params
		} 
		# Is a custom attribute.
		else {
			my $handler = $class->can($method);
			next unless $handler;
			push(@attributes, [ $class, $method, $code, $params ] );
		}
	}

	return ();
}

sub init {
	return unless $init; # Initialize only once
	
	foreach my $attr (@attributes) {
		my $class	= $attr->[0];
		my $method	= $attr->[1];
		
		# calls:  class->method( code, method, params );
		$class->$method( $attr->[2], $attr->[1], $attr->[3]);
	}
	$init = 0;
}


1;
__END__