| 1 | package MooseX::JSONSchema; | ||||||||||||||||||||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:GETTY'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Adding JSON Schema capabilities to your Moose class | ||||||||||||||||||||||||||||
| 4 | 3 | $MooseX::JSONSchema::VERSION = '0.001'; | |||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | 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 | ▶ | sub json_schema_id { shift->json_schema_id(shift) } | ||||||||||||||||||||||||||
|
Subroutine
json_schema_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | sub json_schema_title { shift->json_schema_title(shift) } | ||||||||||||||||||||||||||
|
Subroutine
json_schema_title: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub json_schema_schema { shift->json_schema_schema(shift) } | ||||||||||||||||||||||||||
|
Subroutine
json_schema_schema: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub array { add_json_schema_attribute( array => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
array: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | sub string { add_json_schema_attribute( string => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub object { add_json_schema_attribute( object => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
object: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub number { add_json_schema_attribute( number => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
number: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | sub integer { add_json_schema_attribute( integer => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
integer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub boolean { add_json_schema_attribute( boolean => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
boolean: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 8 | ▶ | sub add_json_schema_attribute { | ||||||||||||||||||||||||||
|
Subroutine
add_json_schema_attribute: called CC=11 SCAR=29.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 8 | my ( $type, $meta, $name, $description, @args ) = @_; | |||||||||||||||||||||||||||
| 35 | 8 | my $subtype; | |||||||||||||||||||||||||||
| 36 | 8 | ▶ | if ($type eq 'array' or $type eq 'object') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$type eq "array" or $type eq "object"
MC/DC0%0/2
$type eq "array" or $type eq "object"
$type eq "array"
$type eq "object"
Truth table33%1/3
A or B
A= $type eq "array"B= $type eq "object"
| |||||||||||||||||||||||||||||
| 37 | 0 | $subtype = shift @args; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 8 | ▶ | my %opts = ( | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($opts{traits}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | push @{$opts{traits}}, 'MooseX::JSONSchema::AttributeTrait'; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 8 | $opts{traits} = ['MooseX::JSONSchema::AttributeTrait']; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 8 | my %context = Moose::Util::_caller_info; | |||||||||||||||||||||||||||
| 58 | 8 | $context{context} = 'moosex jsonschema attribute declaration'; | |||||||||||||||||||||||||||
| 59 | 8 | $context{type} = 'class'; | |||||||||||||||||||||||||||
| 60 | 8 | my @options = ( definition_context => \%context, %opts ); | |||||||||||||||||||||||||||
| 61 | 8 | ▶ | my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 8 | $meta->add_attribute( $_, @options ) for @$attrs; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | 1; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||