File Coverage

blib/lib/MooseX/JSONSchema/MetaClassTrait.pm
Criterion Covered Total %
statement 23 28 82.1
branch n/a
condition n/a
subroutine 7 8 87.5
pod 0 1 0.0
total 30 37 81.0


line stmt bran cond sub pod time code
1             package MooseX::JSONSchema::MetaClassTrait;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: Trait for meta classes having a JSON Schema
4             $MooseX::JSONSchema::MetaClassTrait::VERSION = '0.001';
5 3     3   13395 use Moose::Role;
  3         15733  
  3         12  
6 3     3   22067 use JSON::MaybeXS;
  3         38718  
  3         2127  
7              
8             has json_schema_id => (
9             is => 'rw',
10             isa => 'Str',
11             lazy_build => 1,
12             );
13             sub _build_json_schema_id {
14 3     3   7 my ( $self ) = @_;
15 3         20 my $class = lc($self->name);
16 3         8 $class =~ s/::/./g;
17 3         108 return 'https://json-schema.org/perl.'.$class.'.schema.json';
18             }
19              
20             has json_schema_schema => (
21             is => 'rw',
22             isa => 'Str',
23             lazy_build => 1,
24             );
25             sub _build_json_schema_schema {
26 3     3   6 my ( $self ) = @_;
27 3         127 return 'https://json-schema.org/draft/2020-12/schema';
28             }
29              
30             has json_schema_title => (
31             is => 'rw',
32             isa => 'Str',
33             lazy_build => 1,
34             );
35             sub _build_json_schema_title {
36 0     0   0 my ( $self ) = @_;
37 0         0 my $class = ref $self;
38 0         0 $class =~ s/::/ /g;
39 0         0 return join(' ',map { ucfirst } split(/\s+/, $class));
  0         0  
40             }
41              
42             has json_schema_data => (
43             is => 'ro',
44             isa => 'HashRef',
45             lazy_build => 1,
46             );
47             sub _build_json_schema_data {
48 3     3   7 my ( $self ) = @_;
49             return {
50 3         121 '$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             has json_schema_properties => (
59             is => 'ro',
60             isa => 'HashRef',
61             lazy_build => 1,
62             );
63             sub _build_json_schema_properties {
64 5     5   11 my ( $self ) = @_;
65 5         54 my @schema_attributes = grep { $_->does('MooseX::JSONSchema::AttributeTrait') } $self->get_all_attributes;
  15         2890  
66 5         1195 return { map { $_->name, $_->json_schema_property_data } @schema_attributes };
  15         699  
67             }
68              
69             sub json_schema_json {
70 3     3 0 355364 my ( $self, %args ) = @_;
71 3         148 my $data = $self->json_schema_data;
72 3         45 my $json = JSON::MaybeXS->new(
73             utf8 => 1,
74             canonical => 1,
75             %args,
76             );
77 3         154 return $json->encode($data);
78             }
79              
80             1;
81              
82             __END__
83              
84             =pod
85              
86             =head1 NAME
87              
88             MooseX::JSONSchema::MetaClassTrait - Trait for meta classes having a JSON Schema
89              
90             =head1 VERSION
91              
92             version 0.001
93              
94             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
95              
96             =head1 SUPPORT
97              
98             =head2 Source Code
99              
100             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
101             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
102             from your repository :)
103              
104             L<https://github.com/Getty/perl-moosex-jsonschema>
105              
106             git clone https://github.com/Getty/perl-moosex-jsonschema.git
107              
108             =head1 AUTHOR
109              
110             Torsten Raudssus <torsten@raudss.us>
111              
112             =head1 COPYRIGHT AND LICENSE
113              
114             This software is copyright (c) 2024 by Torsten Raudssus.
115              
116             This is free software; you can redistribute it and/or modify it under
117             the same terms as the Perl 5 programming language system itself.
118              
119             =cut