JMAP-Tester

 view release on metacpan or  search on metacpan

lib/JMAP/Tester/Response/Sentence/Set.pm  view on Meta::CPAN

#pod =method created_creation_ids
#pod
#pod This returns the list of creation ids that were successfully created.  Note:
#pod this returns I<creation ids>, not object ids.
#pod
#pod =method created_ids
#pod
#pod This returns the list of object ids that were successfully created.
#pod
#pod =method not_created_ids
#pod
#pod This returns the list of creation ids that were I<not> successfully created.
#pod
#pod =method create_errors
#pod
#pod This returns a hashref mapping creation ids to error properties.
#pod
#pod =method updated_ids
#pod
#pod This returns a list of object ids that were successfully updated.
#pod
#pod =method not_updated_ids
#pod
#pod This returns a list of object ids that were I<not> successfully updated.
#pod
#pod =method update_errors
#pod
#pod This returns a hashref mapping object ids to error properties.
#pod
#pod =method destroyed_ids
#pod
#pod This returns a list of object ids that were successfully destroyed.
#pod
#pod =method not_destroyed_ids
#pod
#pod This returns a list of object ids that were I<not> successfully destroyed.
#pod
#pod =method destroy_errors
#pod
#pod This returns a hashref mapping object ids to error properties.
#pod
#pod =cut

sub as_set ($self) { $self }

sub created ($self) { $self->arguments->{created} // {} }

sub created_id ($self, $creation_id) {
  return undef unless my $props = $self->created->{$creation_id};
  return $props->{id};
}

sub created_creation_ids ($self) {
  keys %{ $self->created }
}

sub created_ids ($self) {
  map {; $_->{id} } values %{ $self->created }
}

sub updated_ids ($self) {
  my $updated = $self->{arguments}{updated} // {};

  if (ref $updated eq 'ARRAY') {
    return @$updated;
  }

  return keys %$updated;
}

sub updated ($self) {
  my $updated = $self->{arguments}{updated} // {};

  if (ref $updated eq 'ARRAY') {
    return { map {; $_ => undef } @$updated };
  }

  return $updated;
}

sub destroyed_ids ($self) { @{ $self->{arguments}{destroyed} } }

# Is this the best API to provide?  I dunno, maybe.  Usage will tell us whether
# it's right. -- rjbs, 2016-04-11
sub not_created_ids   ($self) { keys %{ $self->{arguments}{notCreated} }   }
sub not_updated_ids   ($self) { keys %{ $self->{arguments}{notUpdated} }   }
sub not_destroyed_ids ($self) { keys %{ $self->{arguments}{notDestroyed} } }

sub create_errors     ($self) { $self->{arguments}{notCreated}   // {} }
sub update_errors     ($self) { $self->{arguments}{notUpdated}   // {} }
sub destroy_errors    ($self) { $self->{arguments}{notDestroyed} // {} }

sub assert_no_errors ($self) {
  my @errors;
  local $Data::Dumper::Terse = 1;

  if (keys %{ $self->create_errors }) {
    push @errors, "notCreated: " .  Data::Dumper::Dumper(
      $self->_strip_json_types( $self->create_errors )
    );
  }

  if (keys %{ $self->update_errors }) {
    push @errors, "notUpdated: " .  Data::Dumper::Dumper(
      $self->_strip_json_types( $self->update_errors )
    );
  }

  if (keys %{ $self->destroy_errors }) {
    push @errors, "notDestroyed: " .  Data::Dumper::Dumper(
      $self->_strip_json_types( $self->destroy_errors )
    );
  }

  return $self unless @errors;

  $self->sentence_broker->abort(
    "errors found in " . $self->name . " sentence",
    \@errors,
  );
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

JMAP::Tester::Response::Sentence::Set - the kind of sentence you get in reply to a setFoos call

=head1 VERSION

version 0.109

=head1 OVERVIEW

A "Set" sentence is a kind of L<Sentence|JMAP::Tester::Response::Sentence>
for representing C<foosSet> results.  It has convenience methods for getting
out the data returned in these kinds of sentences.

=head1 PERL VERSION

This library should run on perls released even a long time ago.  It should
work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the



( run in 0.863 second using v1.01-cache-2.11-cpan-98e64b0badf )