| 1 | package WWW::MenuGrinder::Plugin::Variables; | ||
| 2 | our $VERSION = '0.01_01'; | ||
| 3 | |||
| 4 | |||
| 5 | # ABSTRACT: WWW::MenuGrinder plugin that does variable substitutions and checks. | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | use List::Util; | ||
| 9 | |||
| 10 | with 'WWW::MenuGrinder::Role::ItemMogrifier'; | ||
| 11 | |||
| 12 | sub plugin_depends { qw(Visitor) } | ||
| 13 | |||
| 14 | sub plugin_required_grinder_methods { qw(get_variable) } | ||
| 15 | |||
| 16 | has 'substitute_fields' => ( | ||
| 17 | is => 'ro', | ||
| 18 | default => sub { [ 'label' ] } | ||
| 19 | ); | ||
| 20 | |||
| 21 | sub get_var { | ||
| 22 | my ($self, $varname) = @_; | ||
| 23 | |||
| 24 | return $self->grinder->get_variable($varname); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub get_defined_var { | ||
| 28 | my ($self, $varname) = @_; | ||
| 29 | |||
| 30 | my $value = $self->grinder->get_variable($varname); | ||
| 31 | warn "Menu variable '$varname' was undefined in substitution." unless defined $value; | ||
| 32 | return $value; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub item_mogrify { | ||
| 36 | my ($self, $item) = @_; | ||
| 37 | |||
| 38 | if (exists $item->{need_var}) { | ||
| 39 | my @vars = ref($item->{need_var}) ? | ||
| 40 | @{ $item->{need_var} } : $item->{need_var}; | ||
| 41 | for my $var (@vars) { | ||
| 42 | if (!defined $self->get_var($var) ) { | ||
| 43 | return (); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | if (exists $item->{no_var}) { | ||
| 49 | my @vars = ref($item->{no_var}) ? | ||
| 50 | @{ $item->{no_var} } : $item->{no_var}; | ||
| 51 | for my $var (@vars) { | ||
| 52 | if (defined $self->get_var($var) ) { | ||
| 53 | return (); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | for my $field (@{ $self->substitute_fields }) { | ||
| 59 | next unless exists $item->{$field}; | ||
| 60 | |||
| 61 | $item->{$field} =~ s/\${([^}]+)}/$self->get_defined_var($1)/eg; | ||
| 62 | } | ||
| 63 | |||
| 64 | return $item; | ||
| 65 | } | ||
| 66 | |||
| 67 | no Moose; | ||
| 68 | 1; | ||
| 69 | |||
| 70 | |||
| 71 | __END__ | ||
| 72 | =head1 NAME | ||
| 73 | |||
| 74 | WWW::MenuGrinder::Plugin::Variables - WWW::MenuGrinder plugin that does variable substitutions and checks. | ||
| 75 | |||
| 76 | =head1 VERSION | ||
| 77 | |||
| 78 | version 0.01_01 | ||
| 79 | |||
| 80 | =head1 DESCRIPTION | ||
| 81 | |||
| 82 | C<WWW::MenuGrinder::Plugin::Variables> is a plugin for C<WWW::MenuGrinder>. You | ||
| 83 | should not use it directly, but include it in the C<plugins> section of a | ||
| 84 | C<WWW::MenuGrinder> config. | ||
| 85 | |||
| 86 | When loaded, this plugin will interpolate named variables into menu fields from | ||
| 87 | the application context. It will also remove any item containing a C<need_var> | ||
| 88 | key naming a variable that does exist (and all of that item's descendents), as | ||
| 89 | well as any item containing a C<no_var> key naming a variable that I<does> exist | ||
| 90 | (and all of that item's descendents). | ||
| 91 | |||
| 92 | The variable interpolation syntax is akin to Perl's, except that curly braces | ||
| 93 | are mandatory. For example, the string C<"Hello, ${object}!"> becomes | ||
| 94 | C<"Hello, world!"> if the variable C<object> holds the value C<"world">. | ||
| 95 | |||
| 96 | =head2 Configuration | ||
| 97 | |||
| 98 | =over 4 | ||
| 99 | |||
| 100 | =item * C<substitute_fields> | ||
| 101 | |||
| 102 | An arrayref containing the names of menu keys to perform variable substitution | ||
| 103 | on. Defaults to C<['label']>. | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | =head2 Required Methods | ||
| 108 | |||
| 109 | In order to load this plugin your C<WWW::MenuGrinder> subclass must implement | ||
| 110 | the method C<get_variable> accepting a variable name (without C<${}>) and | ||
| 111 | returning the value of that variable, or undef if the variable does not exist or | ||
| 112 | is not set. Existence and definedness are not distinguished. The source of | ||
| 113 | "variables" is left entirely to the implementer, but might be the stash, the | ||
| 114 | session, the application configuration, or some combination. | ||
| 115 | |||
| 116 | =head1 AUTHOR | ||
| 117 | |||
| 118 | Andrew Rodland <andrew@hbslabs.com> | ||
| 119 | |||
| 120 | =head1 COPYRIGHT AND LICENSE | ||
| 121 | |||
| 122 | This software is copyright (c) 2009 by HBS Labs, LLC.. | ||
| 123 | |||
| 124 | This is free software; you can redistribute it and/or modify it under | ||
| 125 | the same terms as perl itself. | ||
| 126 |