| 1 | package ICANN::RST::Input; | ||
| 2 | # ABSTRACT: an object representing an RST input parameter. | ||
| 3 | 1 | ▶ | use base qw(ICANN::RST::Base); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use JSON::Schema; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use strict; | ||
| 6 | |||
| 7 | sub description { ICANN::RST::Text->new($_[0]->{'Description'}) } | ||
| 8 | sub type { $_[0]->{'Type'} } | ||
| 9 | sub example { numify($_[0]->{'Example'}) } | ||
| 10 | sub schema { $_[0]->{'Schema'} ? JSON::Schema->new(numify($_[0]->{'Schema'})) : undef } | ||
| 11 | sub required { exists($_[0]->{'Required'}) && $_[0]->{'Required'} } | ||
| 12 | |||
| 13 | sub jsonExample { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | if ('boolean' eq $self->{'Schema'}->{'type'}) { | ||
| 17 | return ($self->example ? \1 : \0); | ||
| 18 | |||
| 19 | } else { | ||
| 20 | return numify($self->example); | ||
| 21 | |||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | sub cases { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | my %cases; | ||
| 29 | |||
| 30 | foreach my $case ($self->spec->cases) { | ||
| 31 | foreach my $input ($case->inputs) { | ||
| 32 | $cases{$case->id} = $case if ($input->id eq $self->id && !defined($cases{$case->id})); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | return sort { $a->id cmp $b->id } values(%cases); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub suites { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | my %suites; | ||
| 43 | |||
| 44 | foreach my $suite ($self->spec->suites) { | ||
| 45 | foreach my $id (@{$suite->{'Input-Parameters'}}) { | ||
| 46 | $suites{$suite->id} = $suite if ($id eq $self->id && !defined($suites{$suite->id})); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | return sort { $a->order cmp $b->order } values(%suites); | ||
| 51 | } | ||
| 52 | |||
| 53 | # | ||
| 54 | # iteratively convert scalars into numeric values where appropriate | ||
| 55 | # | ||
| 56 | sub numify { | ||
| 57 | my $value = shift; | ||
| 58 | |||
| 59 | if ('HASH' eq ref($value)) { | ||
| 60 | return numify_hash($value); | ||
| 61 | |||
| 62 | } elsif ('ARRAY' eq ref($value)) { | ||
| 63 | return numify_array($value); | ||
| 64 | |||
| 65 | } else { | ||
| 66 | return numify_scalar($value); | ||
| 67 | |||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub numify_hash { | ||
| 72 | my $ref = shift; | ||
| 73 | |||
| 74 | my $ref2 = {}; | ||
| 75 | foreach my $key (keys(%{$ref})) { | ||
| 76 | $ref2->{$key} = numify($ref->{$key}); | ||
| 77 | } | ||
| 78 | |||
| 79 | return $ref2; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub numify_array { | ||
| 83 | my $ref = shift; | ||
| 84 | |||
| 85 | my @array; | ||
| 86 | foreach my $value (@{$ref}) { | ||
| 87 | push (@array, numify($value)); | ||
| 88 | } | ||
| 89 | |||
| 90 | return \@array; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub numify_scalar { | ||
| 94 | my $value = shift; | ||
| 95 | |||
| 96 | if (q{JSON::PP::Boolean} ne ref($value) && $value =~ /^\d+\.?\d*$/) { | ||
| 97 | $value += 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | return $value; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ | ||