| 1 | package UR::AttributeHandlers; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use attributes; | ||
| 6 | |||
| 7 | our @CARP_NOT = qw(UR::Namespace); | ||
| 8 | |||
| 9 | our $VERSION = "0.46"; # UR $VERSION;; | ||
| 10 | |||
| 11 | # implement's UR's mechanism for sub/variable attributes. | ||
| 12 | my %support_functions = ( | ||
| 13 | MODIFY_CODE_ATTRIBUTES => \&modify_attributes, | ||
| 14 | FETCH_CODE_ATTRIBUTES => \&fetch_attributes, | ||
| 15 | MODIFY_SCALAR_ATTRIBUTES => \&modify_attributes, | ||
| 16 | ); | ||
| 17 | |||
| 18 | sub import_support_functions_to_package { | ||
| 19 | my $package = shift; | ||
| 20 | |||
| 21 | while( my($name, $code) = each %support_functions ) { | ||
| 22 | my $target = join('::', $package, $name); | ||
| 23 | do { | ||
| 24 | no strict 'refs'; | ||
| 25 | *$target = $code; | ||
| 26 | }; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | my %modify_attribute_handlers = ( | ||
| 32 | CODE => { Overrides => \&modify_code_overrides }, | ||
| 33 | SCALAR => { RoleParam => \&modify_scalar_role_property }, | ||
| 34 | ); | ||
| 35 | my %fetch_attribute_handlers = ( | ||
| 36 | CODE => { Overrides => \&fetch_code_overrides }, | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub _modify_attribute_handler { | ||
| 40 | my($ref, $attr) = @_; | ||
| 41 | my $reftype = attributes::reftype($ref); | ||
| 42 | return (exists($modify_attribute_handlers{$reftype}) and $modify_attribute_handlers{$reftype}->{$attr}); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _fetch_attribute_handler { | ||
| 46 | my($ref, $attr) = @_; | ||
| 47 | my $reftype = attributes::reftype($ref); | ||
| 48 | return (exists($fetch_attribute_handlers{$reftype}) and $fetch_attribute_handlers{$reftype}->{$attr}); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _decompose_attr { | ||
| 52 | my($raw_attr) = @_; | ||
| 53 | my($attr, $params_str) = $raw_attr =~ m/^(\w+)(?:\((.*)\))$/; | ||
| 54 | |||
| 55 | my @params = defined($params_str) ? split(/\s*,\s*/, $params_str) : (); | ||
| 56 | $attr = $raw_attr unless defined $attr; | ||
| 57 | return ($attr, @params); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub modify_attributes { | ||
| 61 | my($package, $ref, @raw_attrs) = @_; | ||
| 62 | |||
| 63 | my @not_recognized; | ||
| 64 | foreach my $raw_attr ( @raw_attrs ) { | ||
| 65 | my($attr, @params) = _decompose_attr($raw_attr); | ||
| 66 | if (my $handler = _modify_attribute_handler($ref, $attr)) { | ||
| 67 | $handler->($package, $ref, $attr, @params); | ||
| 68 | } else { | ||
| 69 | push @not_recognized, $raw_attr; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | return @not_recognized; | ||
| 74 | } | ||
| 75 | |||
| 76 | my %stored_attributes_by_ref; | ||
| 77 | |||
| 78 | sub fetch_attributes { | ||
| 79 | my($package, $ref) = @_; | ||
| 80 | |||
| 81 | my $reftype = attributes::reftype($ref); | ||
| 82 | my @attrs; | ||
| 83 | foreach my $attr ( keys %{ $stored_attributes_by_ref{$ref} } ) { | ||
| 84 | if (my $handler = _fetch_attribute_handler($ref, $attr)) { | ||
| 85 | push @attrs, $handler->($package, $ref); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | return @attrs; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub modify_code_overrides { | ||
| 92 | my($package, $coderef, $attr, @params) = @_; | ||
| 93 | |||
| 94 | my $list = $stored_attributes_by_ref{$coderef}->{overrides} ||= []; | ||
| 95 | push @$list, @params; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub modify_scalar_role_property { | ||
| 99 | my($package, $scalar_ref, $attr, $name) = @_; | ||
| 100 | |||
| 101 | unless ($name) { | ||
| 102 | Carp::croak('RoleParam attribute requires a name in parens. For example: my $var : RoleParam(var)'); | ||
| 103 | } | ||
| 104 | $$scalar_ref = UR::Role::Param->new(name => $name, role_name => $package, varref => $scalar_ref); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub fetch_code_overrides { | ||
| 108 | my($package, $coderef) = @_; | ||
| 109 | |||
| 110 | return sprintf('overrides(%s)', | ||
| 111 | join(', ', @{ $stored_attributes_by_ref{$coderef}->{overrides} })); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub get_overrides_for_coderef { | ||
| 115 | my($ref) = @_; | ||
| 116 | return( exists($stored_attributes_by_ref{$ref}) && exists($stored_attributes_by_ref{$ref}->{overrides}) | ||
| 117 | ? @{ $stored_attributes_by_ref{$ref}->{overrides} } | ||
| 118 | : () | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; |