Skip to content

Commit

Permalink
Item14237: Recording of sugar's owner
Browse files Browse the repository at this point in the history
  • Loading branch information
vrurg committed May 18, 2018
1 parent f615b55 commit efb6a0d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions core/lib/Foswiki/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,18 @@ my %_optionSet = (
);

# Predefined sugars
# Format:
# -option => {
# code => \&handlerSub, # Sub implementing the sugar
# owner => 'Foswiki::Module', # What module registered it.
# }
my %_sugars = (
-sugar => { newSugar => { code => \&registerSugar, } },
-default => { stubMethods => { code => \&_handler_stubMethods }, },
-sugar =>
{ newSugar => { code => \&registerSugar, owner => __PACKAGE__, } },
-default => {
stubMethods =>
{ code => \&_handler_stubMethods, owner => __PACKAGE__, },
},
);

# Prepare %_sugars for use.
Expand Down Expand Up @@ -586,20 +595,28 @@ sub _rebuildSugars {
sub registerSugar (%) {
my %params = @_;

my $owner = caller;

while ( my ( $option, $optData ) = each %params ) {
while ( my ( $sgName, $sgParam ) = each %$optData ) {

die "Bad parameters type for new sugar "
. $sgName
. ": must be a hash or code ref but got "
. ( ref($sgParam) || ( defined $sgParam ? 'SCALAR' : '*undef*' ) )
unless ref($sgParam) && ref($sgParam) =~ /^(?:CODE|HASH)$/;

$sgParam = { code => $sgParam } if ref($sgParam) eq 'CODE';

die "Sugar $sgName code either not defined or not a coderef"
unless defined $sgParam->{code}
&& ref( $sgParam->{code} ) eq 'CODE';

die "Trying to re-register sugar $sgName"
if exists $_sugars{$option}{$sgName};

$sgParam->{owner} //= $owner;

$_sugars{$option}{$sgName} = $sgParam;
push @{ $_optionSet{$option}{sugars} }, $sgName;
}
Expand Down

0 comments on commit efb6a0d

Please sign in to comment.