| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # ABSTRACT: Javascript Object Rendering for Validation::Class | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Validation::Class::Plugin::JavascriptObjects; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 30363 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 6 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 1377 | use JSON -convert_blessed_universally; | 
|  | 1 |  |  |  |  | 19476 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 1437 | use Validation::Class::Util; | 
|  | 1 |  |  |  |  | 4729 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '7.900021'; # VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub new { | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 0 |  |  | 0 | 0 |  | my $class     = shift; | 
| 18 | 0 |  |  |  |  |  | my $prototype = shift; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 0 |  |  |  |  |  | my $self = {prototype => $prototype}; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 |  |  |  |  |  | return bless $self, $class; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub proto { | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 |  |  | 0 | 0 |  | goto &prototype; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub prototype { | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  | 0 | 0 |  | my ($self) = @_; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  |  | return $self->{prototype}; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub render { | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  | 0 | 1 |  | my ($self, %options)  = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | my $model     = $self->prototype; | 
| 46 | 0 |  | 0 |  |  |  | my $namespace = $options{namespace} || $model->package || 'object'; | 
| 47 | 0 |  |  |  |  |  | my $next      = my $root = {}; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  |  |  |  | $next = $next->{$_} = {} for split /\W+/, $namespace; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 | 0 |  |  |  |  | my @fields = isa_arrayref($options{fields}) ? | 
| 52 | 0 | 0 |  |  |  |  | map { $model->fields->get($_) || () } @{$options{fields}} : | 
|  | 0 |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | $model->fields->values | 
| 54 |  |  |  |  |  |  | ; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 |  |  |  |  |  | foreach my $field (@fields) { | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 0 | 0 |  |  |  |  | my %data = map { | 
| 59 |  |  |  |  |  |  | # automatically excludes validation, etc | 
| 60 | 0 |  |  |  |  |  | isa_coderef($field->{$_}) ? () : ($_ => $field->{$_}) | 
| 61 |  |  |  |  |  |  | }   $field->keys; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | my $name = $field->name; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 | 0 |  |  |  |  | if (isa_arrayref $options{include}) { | 
| 66 | 0 |  |  |  |  |  | %data = map { $_ => $data{$_} } @{$options{include}}; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 | 0 |  |  |  |  | if (isa_arrayref $options{exclude}) { | 
| 70 | 0 |  |  |  |  |  | delete $data{$_} for @{$options{exclude}}; | 
|  | 0 |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 |  | 0 |  |  |  | $next->{$name} ||= {%data}; | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | # generate the JS object | 
| 78 | 0 |  |  |  |  |  | my @data = each(%{$root}); | 
|  | 0 |  |  |  |  |  |  | 
| 79 | 0 |  |  |  |  |  | my $data = sprintf 'var %s = %s;', $data[0], JSON->new | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | ->allow_nonref | 
| 82 |  |  |  |  |  |  | ->allow_blessed | 
| 83 |  |  |  |  |  |  | ->convert_blessed | 
| 84 |  |  |  |  |  |  | ->utf8->pretty | 
| 85 |  |  |  |  |  |  | ->encode($data[1]) | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | ; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 0 |  |  |  |  |  | return $data; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | 1; | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | __END__ |