| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 1120 | use utf8; | 
|  | 1 |  |  |  |  | 14 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package JSON::API::v1::Attribute; | 
| 4 |  |  |  |  |  |  | our $VERSION = '0.001'; | 
| 5 | 1 |  |  | 1 |  | 676 | use Moose; | 
|  | 1 |  |  |  |  | 474016 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 6 | 1 |  |  | 1 |  | 7804 | use namespace::autoclean; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 12 |  | 
| 7 | 1 |  |  | 1 |  | 150 | use Carp qw(croak); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 74 |  | 
| 8 | 1 |  |  | 1 |  | 8 | use List::Util qw(any); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 524 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our @CARP_NOT; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | # ABSTRACT: A JSON API Attribute object according to jsonapi v1 specification | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | has attributes => ( | 
| 15 |  |  |  |  |  |  | is      => 'ro', | 
| 16 |  |  |  |  |  |  | isa     => 'HashRef', | 
| 17 |  |  |  |  |  |  | traits  => ['Hash'], | 
| 18 |  |  |  |  |  |  | lazy    => 1, | 
| 19 |  |  |  |  |  |  | default => sub { {} }, | 
| 20 |  |  |  |  |  |  | handles => { | 
| 21 |  |  |  |  |  |  | has_attribute       => 'exists', | 
| 22 |  |  |  |  |  |  | get_attribute       => 'get', | 
| 23 |  |  |  |  |  |  | set_attribute       => 'set', | 
| 24 |  |  |  |  |  |  | add_attribute       => 'set', | 
| 25 |  |  |  |  |  |  | clear_attribute     => 'delete', | 
| 26 |  |  |  |  |  |  | get_attribute_names => 'keys', | 
| 27 |  |  |  |  |  |  | }, | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub TO_JSON { | 
| 31 | 3 |  |  | 3 | 0 | 96 | my $self = shift; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 3 |  |  |  |  | 6 | my %rv; | 
| 34 | 3 |  |  |  |  | 117 | foreach ($self->get_attribute_names) { | 
| 35 | 3 |  |  |  |  | 14 | $rv{$_} = $self->get_attribute($_); | 
| 36 |  |  |  |  |  |  | } | 
| 37 | 2 |  |  |  |  | 28 | return \%rv; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | my @forbidden = qw(links relationships); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | sub _assert_attribute_name { | 
| 43 | 7 |  |  | 7 |  | 15 | my ($self, $key) = @_; | 
| 44 | 7 | 100 | 50 | 11 |  | 35 | if (any { $_ eq ($key // '') } @forbidden) { | 
|  | 11 |  |  |  |  | 37 |  | 
| 45 | 5 |  |  |  |  | 13 | local @CARP_NOT = qw( | 
| 46 |  |  |  |  |  |  | Class::MOP::Method::Wrapped | 
| 47 |  |  |  |  |  |  | ); | 
| 48 | 5 |  |  |  |  | 58 | croak("Unable to use reserved keyword '$key'!"); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | around set_attribute => sub { | 
| 53 |  |  |  |  |  |  | my ($orig, $self, $key, @rest) = @_; | 
| 54 |  |  |  |  |  |  | $self->_assert_attribute_name($key); | 
| 55 |  |  |  |  |  |  | return $self->$orig($key, @rest); | 
| 56 |  |  |  |  |  |  | }; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | around get_attribute => sub { | 
| 59 |  |  |  |  |  |  | my ($orig, $self, $key) = @_; | 
| 60 |  |  |  |  |  |  | $self->_assert_attribute_name($key); | 
| 61 |  |  |  |  |  |  | return $self->$orig($key); | 
| 62 |  |  |  |  |  |  | }; | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | with qw( | 
| 65 |  |  |  |  |  |  | JSON::API::v1::Roles::TO_JSON | 
| 66 |  |  |  |  |  |  | ); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | __END__ | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =pod | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =encoding UTF-8 | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =head1 NAME | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | JSON::API::v1::Attribute - A JSON API Attribute object according to jsonapi v1 specification | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | =head1 VERSION | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | version 0.001 | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | This module attempts to make a Moose object behave like a JSON API object as | 
| 90 |  |  |  |  |  |  | defined by L<jsonapi.org>. This object adheres to the v1 specification | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head1 METHODS | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 AUTHOR | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Wesley Schwengle <waterkip@cpan.org> | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | This software is Copyright (c) 2020 by Wesley Schwengle. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | This is free software, licensed under: | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | The (three-clause) BSD License | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =cut |