Catalyst-View-TT-FunctionGenerator

 view release on metacpan or  search on metacpan

lib/Catalyst/View/TT/FunctionGenerator.pm  view on Meta::CPAN

#!/usr/bin/perl

package Catalyst::View::TT::FunctionGenerator;
use base qw/Catalyst::View::TT/;

use strict;
use warnings;

use Catalyst::Utils ();
use Class::Inspector ();
use Scalar::Util qw/weaken/;
use Carp ();

our $VERSION = "0.02";

__PACKAGE__->mk_accessors(qw/context/);

sub ACCEPT_CONTEXT {
	my ( $self, $c ) = @_;
	bless { %$self, context => $c }, ref $self;
}

lib/Catalyst/View/TT/FunctionGenerator.pm  view on Meta::CPAN

        if ( ref $meths eq "ARRAY" ) {
            if ( not ref $meths->[0] ) {
                my ( $name, @methods ) = @$meths;
                $meths = [ $c->$name => @methods ];
            }
        } else {
            $meths = [ $meths, @{ Class::Inspector->methods( ( ref $meths || $meths ), 'public' ) || Carp::croak("$meths has no methods") } ];
        }

        # if there is a closure with $c in it, and it's saved inside $c we have a circular referrence
        weaken($meths->[0]) if ( $meths->[0] == $c );

        $meths;
    } @objects;
}

# for each item passed to sub, check if its an arrayref, if it is, and the first
# item in it is not a ref, then assume its the name of a method on $c, and
# call it to get the object. Else assume an entire object (This will end up
# with an [name, (method names)] if you pass in a string?). Return an arrayref,
# which gets added to the evil list.

sub template_vars {
    my ( $self, $c ) = @_;

    return (
        $self->NEXT::template_vars( $c ),
        map {
            my ( $obj, @methods ) = @$_;
            weaken( $obj ) if ( $obj == $c );
            #see above
            map { my $method = $_; $method => sub { $obj->$method(@_) } } @methods;
        }
        @{ $c->{_evil_function_generator_data}{ref $self} || [] }
    );
}

# for each item (arrayref) in the evil list, create a template var using the
# method names, that calls that method, on the object in the first slot of the array.



( run in 0.255 second using v1.01-cache-2.11-cpan-65fba6d93b7 )