Changeset e26abb6


Ignore:
Timestamp:
Feb 23, 2013, 5:44:21 PM (11 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10
Children:
bbc31e4
Parents:
523146b
git-author:
David Benjamin <davidben@mit.edu> (02/16/13 15:43:20)
git-committer:
David Benjamin <davidben@mit.edu> (02/23/13 17:44:21)
Message:
Add new_variable_enum

This seems to work. Yay.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    r523146b re26abb6  
    1818                    new_command
    1919                    new_variable_int new_variable_bool new_variable_string
     20                    new_variable_enum
    2021                    quote redisplay);
    2122our %EXPORT_TAGS = (all => \@EXPORT_OK);
     
    402403=head2 new_variable_string NAME [{ARGS}]
    403404
    404 Add a new owl variable, either an int, a bool, or a string, with the
     405=head2 new_variable_enum NAME [{ARGS}]
     406
     407Add a new owl variable, either an int, a bool, a string, or an enum with the
    405408specified name.
    406409
    407 ARGS can optionally contain the following keys:
     410For new_variable_enum, ARGS is required to contain a validsettings key pointing
     411to an array reference. For all four, it can optionally contain the following
     412keys:
    408413
    409414=over 4
     
    469474            validsettings => "<string>",
    470475            takes_on_off => 0,
     476        });
     477}
     478
     479sub new_variable_enum {
     480    my ($name, $args) = @_;
     481
     482    # Gather the valid settings.
     483    die "validsettings is required" unless defined($args->{validsettings});
     484    my %valid;
     485    map { $valid{$_} = 1 } @{$args->{validsettings}};
     486
     487    my $storage = (defined($args->{default}) ?
     488                   $args->{default} :
     489                   $args->{validsettings}->[0]);
     490    BarnOwl::new_variable_full($name, {
     491            %{$args},
     492            get_tostring => sub { $storage },
     493            set_fromstring => sub {
     494                return -1 unless $valid{$_[0]};
     495                $storage = $_[0];
     496                return 0;
     497            },
     498            validsettings => join(",", @{$args->{validsettings}})
    471499        });
    472500}
Note: See TracChangeset for help on using the changeset viewer.