Changeset 25089 for lang

Show
Ignore:
Timestamp:
11/27/08 14:47:10 (4 years ago)
Author:
yappo
Message:

lazy_build support

Location:
lang/perl/Shika/trunk
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Shika/trunk/lib/Shika.pm

    r25088 r25089  
    3535            next unless exists $has->{default} && $has->{default}; 
    3636            next if     exists $has->{lazy} && $has->{lazy}; 
     37            next if     exists $has->{lazy_build} && $has->{lazy_build}; 
    3738            my $code = $has->{default}; 
    3839            $attr{$name} = ref($code) eq 'CODE' ? $code->() : $code; 
     
    6566    if (exists $attr{lazy} && $attr{lazy}) { 
    6667        _has_lazy_install($pkg, $n, %attr); 
     68    } elsif (exists $attr{lazy_build} && $attr{lazy_build}) { 
     69        _has_lazy_build_install($pkg, $n, %attr); 
    6770    } else { 
    6871        _has_install($pkg, $n, %attr); 
     
    97100} 
    98101 
     102sub _has_lazy_build_install { 
     103    my($pkg, $n) = @_; 
     104    no strict 'refs'; 
     105    *{"$pkg\::$n"} = sub { 
     106        if (@_ == 1) { 
     107            unless (exists $_[0]->{$n}) { 
     108                die "$pkg does not support builder method '_build_$n' for attribute '$n'" 
     109                    unless my $code = $pkg->can("_build_$n"); 
     110                return $_[0]->{$n} = $code->(); 
     111            } 
     112            return $_[0]->{$n}; 
     113        } 
     114        return $_[0]->{$n} = $_[1] if @_ == 2; 
     115        shift->{$n} = \@_; 
     116    }; 
     117} 
     118 
    99119sub _extends { 
    100120    my $pkg = caller(0);