| 1 | package MooseX::JSONSchema::MetaClassTrait; | ||
| 2 | 3 | our $AUTHORITY = 'cpan:GETTY'; | |
| 3 | # ABSTRACT: Trait for meta classes having a JSON Schema | ||
| 4 | 3 | $MooseX::JSONSchema::MetaClassTrait::VERSION = '0.001'; | |
| 5 | 3 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use JSON::MaybeXS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 3 | has json_schema_id => ( | |
| 9 | is => 'rw', | ||
| 10 | isa => 'Str', | ||
| 11 | lazy_build => 1, | ||
| 12 | ); | ||
| 13 | 3 | ▶ | sub _build_json_schema_id { |
|
Subroutine
_build_json_schema_id: called CC=1 SCAR=0.0
| |||
| 14 | 3 | my ( $self ) = @_; | |
| 15 | 3 | my $class = lc($self->name); | |
| 16 | 3 | $class =~ s/::/./g; | |
| 17 | 3 | return 'https://json-schema.org/perl.'.$class.'.schema.json'; | |
| 18 | } | ||
| 19 | |||
| 20 | 3 | has json_schema_schema => ( | |
| 21 | is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | lazy_build => 1, | ||
| 24 | ); | ||
| 25 | 3 | ▶ | sub _build_json_schema_schema { |
|
Subroutine
_build_json_schema_schema: called CC=1 SCAR=0.0
| |||
| 26 | 3 | my ( $self ) = @_; | |
| 27 | 3 | return 'https://json-schema.org/draft/2020-12/schema'; | |
| 28 | } | ||
| 29 | |||
| 30 | 3 | has json_schema_title => ( | |
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | lazy_build => 1, | ||
| 34 | ); | ||
| 35 | 0 | ▶ | sub _build_json_schema_title { |
|
Subroutine
_build_json_schema_title: not called CC=1 SCAR=6.9
| |||
| 36 | 0 | my ( $self ) = @_; | |
| 37 | 0 | my $class = ref $self; | |
| 38 | 0 | $class =~ s/::/ /g; | |
| 39 | 0 | return join(' ',map { ucfirst } split(/\s+/, $class)); | |
| 40 | } | ||
| 41 | |||
| 42 | 3 | has json_schema_data => ( | |
| 43 | is => 'ro', | ||
| 44 | isa => 'HashRef', | ||
| 45 | lazy_build => 1, | ||
| 46 | ); | ||
| 47 | 3 | ▶ | sub _build_json_schema_data { |
|
Subroutine
_build_json_schema_data: called CC=1 SCAR=0.0
| |||
| 48 | 3 | my ( $self ) = @_; | |
| 49 | return { | ||
| 50 | 3 | '$id' => $self->json_schema_id, | |
| 51 | '$schema' => $self->json_schema_schema, | ||
| 52 | title => $self->json_schema_title, | ||
| 53 | type => 'object', | ||
| 54 | properties => $self->json_schema_properties, | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | |||
| 58 | 3 | has json_schema_properties => ( | |
| 59 | is => 'ro', | ||
| 60 | isa => 'HashRef', | ||
| 61 | lazy_build => 1, | ||
| 62 | ); | ||
| 63 | 5 | ▶ | sub _build_json_schema_properties { |
|
Subroutine
_build_json_schema_properties: called CC=1 SCAR=0.0
| |||
| 64 | 5 | my ( $self ) = @_; | |
| 65 | 5 | my @schema_attributes = grep { $_->does('MooseX::JSONSchema::AttributeTrait') } $self->get_all_attributes; | |
| 66 | 5 | return { map { $_->name, $_->json_schema_property_data } @schema_attributes }; | |
| 67 | } | ||
| 68 | |||
| 69 | 3 | ▶ | sub json_schema_json { |
|
Subroutine
json_schema_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 70 | 3 | my ( $self, %args ) = @_; | |
| 71 | 3 | my $data = $self->json_schema_data; | |
| 72 | 3 | my $json = JSON::MaybeXS->new( | |
| 73 | utf8 => 1, | ||
| 74 | canonical => 1, | ||
| 75 | %args, | ||
| 76 | ); | ||
| 77 | 3 | return $json->encode($data); | |
| 78 | } | ||
| 79 | |||
| 80 | 3 | 1; | |
| 81 | |||
| 82 | __END__ | ||