| 1 | package Chloro::Field; | ||||||||||
| 2 | |||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 12 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 12 | our $VERSION = '0.07'; | |||||||||
| 8 | |||||||||||
| 9 | 12 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 12 | ▶ | use MooseX::StrictConstructor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 12 | ▶ | use Chloro::Types qw( Bool CodeRef NonEmptySimpleStr Str Value ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 12 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 12 | with 'Chloro::Role::FormComponent'; | |||||||||
| 16 | |||||||||||
| 17 | 12 | has type => ( | |||||||||
| 18 | is => 'ro', | ||||||||||
| 19 | isa => 'Moose::Meta::TypeConstraint', | ||||||||||
| 20 | required => 1, | ||||||||||
| 21 | init_arg => 'isa', | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 12 | has default => ( | |||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | isa => Value | CodeRef, | ||||||||||
| 27 | predicate => 'has_default', | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | 12 | has is_required => ( | |||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | isa => Bool, | ||||||||||
| 33 | init_arg => 'required', | ||||||||||
| 34 | default => 0, | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 12 | has is_secure => ( | |||||||||
| 38 | is => 'ro', | ||||||||||
| 39 | isa => Bool, | ||||||||||
| 40 | init_arg => 'secure', | ||||||||||
| 41 | default => 0, | ||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | 12 | has extractor => ( | |||||||||
| 45 | is => 'ro', | ||||||||||
| 46 | isa => NonEmptySimpleStr, | ||||||||||
| 47 | default => '_extract_field_value', | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | 12 | has validator => ( | |||||||||
| 51 | is => 'ro', | ||||||||||
| 52 | isa => NonEmptySimpleStr, | ||||||||||
| 53 | default => '_errors_for_field_value', | ||||||||||
| 54 | ); | ||||||||||
| 55 | |||||||||||
| 56 | 39 | ▶ | override BUILDARGS => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 39 | my $class = shift; | |||||||||
| 58 | |||||||||||
| 59 | 39 | my $p = super(); | |||||||||
| 60 | |||||||||||
| 61 | $p->{isa} | ||||||||||
| 62 | = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint( | ||||||||||
| 63 | 39 | $p->{isa} ); | |||||||||
| 64 | |||||||||||
| 65 | 39 | return $p; | |||||||||
| 66 | 12 | }; | |||||||||
| 67 | |||||||||||
| 68 | 9 | ▶ | sub generate_default { | ||||||||
|
Subroutine
generate_default: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | 9 | my $self = shift; | |||||||||
| 70 | 9 | my $params = shift; | |||||||||
| 71 | 9 | my $prefix = shift; | |||||||||
| 72 | |||||||||||
| 73 | 9 | my $default = $self->default(); | |||||||||
| 74 | |||||||||||
| 75 | 9 | ▶ | return ref $default | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | ? $self->$default( $params, $prefix ) | ||||||||||
| 77 | : $default; | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # The Storable hooks are needed because the Moose::Meta::TypeConstraint object | ||||||||||
| 81 | # contains a code reference, and Storable will just die if it tries to | ||||||||||
| 82 | # serialize it. So we save the type's _name_ and look that up when thawing. | ||||||||||
| 83 | # | ||||||||||
| 84 | # Unfortunately, this requires poking around in the object guts a little bit. | ||||||||||
| 85 | 4 | ▶ | sub STORABLE_freeze { | ||||||||
|
Subroutine
STORABLE_freeze: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 86 | 4 | my $self = shift; | |||||||||
| 87 | |||||||||||
| 88 | 4 | my %copy = %{$self}; | |||||||||
| 89 | |||||||||||
| 90 | 4 | my $type = delete $copy{type}; | |||||||||
| 91 | |||||||||||
| 92 | 4 | return q{}, \%copy, \( $type->name() ); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 4 | ▶ | sub STORABLE_thaw { | ||||||||
|
Subroutine
STORABLE_thaw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 96 | 4 | my $self = shift; | |||||||||
| 97 | 4 | shift; | |||||||||
| 98 | 4 | shift; | |||||||||
| 99 | 4 | my $obj = shift; | |||||||||
| 100 | 4 | my $type = shift; | |||||||||
| 101 | |||||||||||
| 102 | 4 | %{$self} = %{$obj}; | |||||||||
| 103 | |||||||||||
| 104 | $self->{type} | ||||||||||
| 105 | = Moose::Util::TypeConstraints::find_or_create_type_constraint( | ||||||||||
| 106 | 4 | ${$type} ); | |||||||||
| 107 | |||||||||||
| 108 | 4 | return; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | # This exists mostly to make testing easier | ||||||||||
| 112 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||
| 113 | 31 | ▶ | sub dump { | ||||||||
|
Subroutine
dump: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 114 | 31 | my $self = shift; | |||||||||
| 115 | |||||||||||
| 116 | return ( | ||||||||||
| 117 | 31 | ▶ | type => $self->type(), | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | required => $self->is_required(), | ||||||||||
| 119 | secure => $self->is_secure(), | ||||||||||
| 120 | ( $self->has_default() ? ( default => $self->default() ) : () ), | ||||||||||
| 121 | ); | ||||||||||
| 122 | } | ||||||||||
| 123 | ## use critic | ||||||||||
| 124 | |||||||||||
| 125 | 12 | __PACKAGE__->meta()->make_immutable(); | |||||||||
| 126 | |||||||||||
| 127 | 12 | 1; | |||||||||
| 128 | |||||||||||
| 129 | # ABSTRACT: A field in a form | ||||||||||
| 130 | |||||||||||
| 131 | __END__ | ||||||||||