| 1 | package LWP::Authen::OAuth2::Args; | ||
| 2 | |||
| 3 | # ABSTRACT: Args | ||
| 4 | our $VERSION = '0.19'; # VERSION | ||
| 5 | |||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | use Carp qw(croak confess); | ||
| 10 | |||
| 11 | use Exporter qw(import); | ||
| 12 | |||
| 13 | our @EXPORT_OK = qw(extract_option copy_option assert_options_empty); | ||
| 14 | |||
| 15 | # Blessed empty hash. | ||
| 16 | sub new { | ||
| 17 | return bless {}, shift; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub extract_option { | ||
| 21 | my $obj = shift; | ||
| 22 | my $opts = shift; | ||
| 23 | my $name = shift; | ||
| 24 | my $has_default = @_; | ||
| 25 | my $default = shift; | ||
| 26 | |||
| 27 | if (exists $opts->{$name}) { | ||
| 28 | return delete $opts->{$name}; | ||
| 29 | } | ||
| 30 | elsif ($has_default) { | ||
| 31 | return $default; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | croak("'$name' is required, cannot be missing"); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | sub copy_option { | ||
| 39 | my $obj = shift; | ||
| 40 | my $opts = shift; | ||
| 41 | my $name = shift; | ||
| 42 | my $has_default = @_; | ||
| 43 | my $default = shift; | ||
| 44 | |||
| 45 | if (not exists $obj->{$name}) { | ||
| 46 | if (exists $opts->{$name}) { | ||
| 47 | my $value = delete $opts->{$name}; | ||
| 48 | if (defined($value)) { | ||
| 49 | $obj->{$name} = $value; | ||
| 50 | } | ||
| 51 | elsif ($has_default) { | ||
| 52 | $obj->{$name} = $default; | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | croak("'$name' is required, cannot be undef"); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | elsif ($has_default) { | ||
| 59 | $obj->{$name} = $default; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | croak("'$name' is required, cannot be missing"); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | elsif (exists $opts->{$name}) { | ||
| 66 | # This should not be hit, but if it was, it would be confusing | ||
| 67 | confess("Refusing to copy '$name' that is already in hash"); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub assert_options_empty { | ||
| 72 | my ($obj, $opts) = @_; | ||
| 73 | my @keys = sort keys %$opts; | ||
| 74 | if (1 == @keys) { | ||
| 75 | croak("Unexpected parameter: '$keys[0]'"); | ||
| 76 | } | ||
| 77 | elsif (@keys) { | ||
| 78 | my $extra = "'" . (join "', '", @keys) . "'"; | ||
| 79 | croak("Extra parameters: $extra"); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ |