DUMMY.pm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package Trog::Data::DUMMY;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use Carp qw{confess};
  7. use JSON::MaybeXS;
  8. use File::Slurper;
  9. use File::Copy;
  10. use Mojo::File;
  11. use List::Util;
  12. use parent qw{Trog::DataModule};
  13. =head1 WARNING
  14. Do not use this as a production data model. It is *not* safe to race conditions, and is only here for testing.
  15. =cut
  16. our $datastore = 'data/DUMMY.json';
  17. sub lang { 'Perl Regex in Quotemeta' }
  18. sub help { 'https://perldoc.perl.org/functions/quotemeta.html' }
  19. sub read ($self, $count=0) {
  20. confess "Can't find datastore!" unless -f $datastore;
  21. my $slurped = File::Slurper::read_text($datastore);
  22. return JSON::MaybeXS::decode_json($slurped);
  23. }
  24. sub write($self,$data) {
  25. open(my $fh, '>', $datastore) or confess;
  26. print $fh JSON::MaybeXS::encode_json($data);
  27. close $fh;
  28. }
  29. sub delete($self, @posts) {
  30. my $example_posts = $self->read();
  31. foreach my $update (@posts) {
  32. @$example_posts = grep { $_->{id} ne $update->{id} } @$example_posts;
  33. }
  34. $self->write($example_posts);
  35. return 0;
  36. }
  37. 1;