File Coverage

blib/lib/MooseX/JSONSchema.pm
Criterion Covered Total %
statement 21 30 70.0
branch 7 16 43.7
condition 1 3 33.3
subroutine 6 12 50.0
pod 0 10 0.0
total 35 71 49.3


line stmt bran cond sub pod time code
1             package MooseX::JSONSchema;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: Adding JSON Schema capabilities to your Moose class
4             $MooseX::JSONSchema::VERSION = '0.001';
5 3     3   2179186 use Moose::Exporter;
  3         7  
  3         24  
6 3     3   258 use Carp qw( croak );
  3         6  
  3         1820  
7              
8             Moose::Exporter->setup_import_methods(
9             with_meta => [
10             qw( array string object number integer boolean ),
11             qw( json_schema_id json_schema_title json_schema_schema ),
12             ],
13             base_class_roles => ['MooseX::JSONSchema::Role'],
14             class_metaroles => {
15             class => ['MooseX::JSONSchema::MetaClassTrait'],
16             },
17             role_metaroles => {
18             role => ['MooseX::JSONSchema::MetaClassTrait'],
19             },
20             );
21              
22 0     0 0 0 sub json_schema_id { shift->json_schema_id(shift) }
23 5     5 0 67124 sub json_schema_title { shift->json_schema_title(shift) }
24 0     0 0 0 sub json_schema_schema { shift->json_schema_schema(shift) }
25              
26 0     0 0 0 sub array { add_json_schema_attribute( array => @_ ) }
27 6     6 0 17260 sub string { add_json_schema_attribute( string => @_ ) }
28 0     0 0 0 sub object { add_json_schema_attribute( object => @_ ) }
29 0     0 0 0 sub number { add_json_schema_attribute( number => @_ ) }
30 2     2 0 14159 sub integer { add_json_schema_attribute( integer => @_ ) }
31 0     0 0 0 sub boolean { add_json_schema_attribute( boolean => @_ ) }
32              
33             sub add_json_schema_attribute {
34 8     8 0 33 my ( $type, $meta, $name, $description, @args ) = @_;
35 8         16 my $subtype;
36 8 50 33     53 if ($type eq 'array' or $type eq 'object') {
37 0         0 $subtype = shift @args;
38             }
39 8 0       87 my %opts = (
    0          
    50          
    50          
    100          
40             json_schema_description => $description,
41             json_schema_type => $type,
42             predicate => 'has_'.$name,
43             is => 'ro',
44             isa => (
45             $type eq 'string' ? 'Str'
46             : $type eq 'number' ? 'Num'
47             : $type eq 'integer' ? 'Int'
48             : $type eq 'array' ? 'ArrayRef'
49             : $type eq 'object' ? 'HashRef' : croak(__PACKAGE__.' can\'t handle type '.$type)),
50             @args,
51             );
52 8 50       28 if ($opts{traits}) {
53 0         0 push @{$opts{traits}}, 'MooseX::JSONSchema::AttributeTrait';
  0         0  
54             } else {
55 8         23 $opts{traits} = ['MooseX::JSONSchema::AttributeTrait'];
56             }
57 8         57 my %context = Moose::Util::_caller_info;
58 8         207 $context{context} = 'moosex jsonschema attribute declaration';
59 8         17 $context{type} = 'class';
60 8         41 my @options = ( definition_context => \%context, %opts );
61 8 50       53 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
62 8         59 $meta->add_attribute( $_, @options ) for @$attrs;
63             }
64              
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =head1 NAME
72              
73             MooseX::JSONSchema - Adding JSON Schema capabilities to your Moose class
74              
75             =head1 VERSION
76              
77             version 0.001
78              
79             =head1 SYNOPSIS
80              
81             package PersonClass;
82              
83             use Moose;
84             use MooseX::JSONSchema;
85              
86             json_schema_title "A person";
87              
88             string first_name => "The first name of the person";
89             string last_name => "The last name of the person";
90             integer age => "Current age in years", json_schema_args => { minimum => 0, maximum => 200 };
91              
92             1;
93              
94             package CharacterClass;
95              
96             use Moose;
97             use MooseX::JSONSchema;
98              
99             extends 'PersonClass';
100              
101             json_schema_title "Extended person";
102              
103             string job => "The job of the person";
104              
105             1;
106              
107             my $json_schema_json = PersonClass->meta->json_schema_json;
108              
109             my $person = PersonClass->new(
110             first_name => "Peter",
111             last_name => "Parker",
112             age => 21,
113             );
114              
115             my $json_schema_data_json = $person->json_schema_data_json;
116              
117             =head1 DESCRIPTION
118              
119             B<THIS API IS WORK IN PROGRESS>
120              
121             =head1 SUPPORT
122              
123             Repository
124              
125             https://github.com/Getty/perl-moosex-jsonschema
126             Pull request and additional contributors are welcome
127              
128             Issue Tracker
129              
130             https://github.com/Getty/perl-moosex-jsonschema/issues
131              
132             =cut
133              
134             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
135              
136             =head1 SUPPORT
137              
138             =head2 Source Code
139              
140             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
141             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
142             from your repository :)
143              
144             L<https://github.com/Getty/perl-moosex-jsonschema>
145              
146             git clone https://github.com/Getty/perl-moosex-jsonschema.git
147              
148             =head1 AUTHOR
149              
150             Torsten Raudssus <torsten@raudss.us>
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is copyright (c) 2024 by Torsten Raudssus.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             =cut