| 1 | =head1 NAME | ||||||||||||
| 2 | |||||||||||||
| 3 | Froody::Base - base class for Froody classes | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 DESCRIPTION | ||||||||||||
| 6 | |||||||||||||
| 7 | A base class for all Froody classes, provides useful methods. | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 METHODS | ||||||||||||
| 10 | |||||||||||||
| 11 | =over 4 | ||||||||||||
| 12 | |||||||||||||
| 13 | =cut | ||||||||||||
| 14 | |||||||||||||
| 15 | package Froody::Base; | ||||||||||||
| 16 | |||||||||||||
| 17 | 56 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 56 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 56 | ▶ | use Params::Validate (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 56 | ▶ | use Froody::Error; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 56 | ▶ | use Froody::Logger; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 56 | my $logger = get_logger("froody.base"); | |||||||||||
| 25 | |||||||||||||
| 26 | 56 | ▶ | use base qw( Class::Accessor::Chained::Fast ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | 56 | ▶ | use UNIVERSAL::require; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | |||||||||||||
| 30 | 56 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | |||||||||||||
| 32 | =item new() | ||||||||||||
| 33 | |||||||||||||
| 34 | A constructor. Doesn't take any parameters. Throws an exception if some | ||||||||||||
| 35 | are passed. | ||||||||||||
| 36 | |||||||||||||
| 37 | =cut | ||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | sub new | ||||||||||||
| 41 | 612 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 42 | 612 | my $class = shift; | |||||||||||
| 43 | |||||||||||||
| 44 | 612 | ▶ | my $fields = shift || {}; | ||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 45 | |||||||||||||
| 46 | 612 | ▶ | my $self = $class->SUPER::new({(ref($class) ? %$class : ()), | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | %$fields | ||||||||||||
| 48 | }); | ||||||||||||
| 49 | 612 | $self->init; | |||||||||||
| 50 | 612 | return $self; | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | =item init | ||||||||||||
| 54 | |||||||||||||
| 55 | Called from new. Designed to be overridden. Remember to call your | ||||||||||||
| 56 | parent init! | ||||||||||||
| 57 | |||||||||||||
| 58 | =cut | ||||||||||||
| 59 | |||||||||||||
| 60 | 548 | ▶ | sub init { return } | ||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 61 | |||||||||||||
| 62 | =item validate_class(@_, { ... }) | ||||||||||||
| 63 | |||||||||||||
| 64 | A utility wrapper around Params::Validate that also makes sure that it's | ||||||||||||
| 65 | being called as a class method, eg. | ||||||||||||
| 66 | |||||||||||||
| 67 | sub class_method { | ||||||||||||
| 68 | my $class = shift; | ||||||||||||
| 69 | my %args = $class->validate_class(@_, { ... } ) | ||||||||||||
| 70 | ... | ||||||||||||
| 71 | |||||||||||||
| 72 | see L<Params::Validate> for details on the function. | ||||||||||||
| 73 | |||||||||||||
| 74 | =cut | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | sub validate_class { | ||||||||||
|
Subroutine
validate_class: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 77 | 0 | my $class = shift; | |||||||||||
| 78 | 0 | ▶ | Froody::Error->throw("perl.methodcall.class", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | "A class method was called in an instance context") if ref($class); | ||||||||||||
| 80 | 0 | $class->_validate(@_); | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | =item validate_object(@_, { ... }) | ||||||||||||
| 84 | |||||||||||||
| 85 | A utility wrapper around Params::Validate that also makes sure that it's | ||||||||||||
| 86 | being called as an object method, eg. | ||||||||||||
| 87 | |||||||||||||
| 88 | sub object_method { | ||||||||||||
| 89 | my $self = shift; | ||||||||||||
| 90 | my %args = $self->validate_class(@_, { ... } ) | ||||||||||||
| 91 | ... | ||||||||||||
| 92 | |||||||||||||
| 93 | see L<Params::Validate> for details on the function. | ||||||||||||
| 94 | |||||||||||||
| 95 | =cut | ||||||||||||
| 96 | |||||||||||||
| 97 | 19 | ▶ | sub validate_object { | ||||||||||
|
Subroutine
validate_object: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 98 | 19 | my $self = shift; | |||||||||||
| 99 | 19 | ▶ | Froody::Error->throw("perl.methodcall.instance", | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | "A instance method was called in object context ") unless ref($self); | ||||||||||||
| 101 | 19 | $self->_validate(@_); | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | =item validate_either(@_, { ... }) | ||||||||||||
| 105 | |||||||||||||
| 106 | A utility wrapper around Params::Validate that does nothing else, for | ||||||||||||
| 107 | methods that can be class or object methods. | ||||||||||||
| 108 | |||||||||||||
| 109 | sub class_method { | ||||||||||||
| 110 | my $class = shift; | ||||||||||||
| 111 | my %args = $class->validate_either(@_, { ... } ) | ||||||||||||
| 112 | ... | ||||||||||||
| 113 | |||||||||||||
| 114 | see L<Params::Validate> for details on the function. | ||||||||||||
| 115 | |||||||||||||
| 116 | =cut | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | sub validate_either { | ||||||||||
|
Subroutine
validate_either: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||
| 120 | 0 | $self->_validate(@_); | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 19 | ▶ | sub _validate { | ||||||||||
|
Subroutine
_validate: called CC=3 SCAR=11.4
| |||||||||||||
| 124 | 19 | my $class = shift; | |||||||||||
| 125 | 19 | ▶ | my $spec = pop || {}; | ||||||||||
|
Condition50%1/2
pop() || {}
MC/DC0%0/1
pop() || {}
pop()
Truth table50%1/2
A
A= pop()
| |||||||||||||
| 126 | 19 | local $Carp::CarpLevel = 4; # hide internals. | |||||||||||
| 127 | 19 | my %h = eval { Params::Validate::validate(@_, $spec) }; | |||||||||||
| 128 | # TODO - it would be really nice to actually throw meta-information | ||||||||||||
| 129 | # about what is missing, what doesn't validate, etc.. | ||||||||||||
| 130 | 19 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 0 | Froody::Error->throw("perl.methodcall.param", $@); | |||||||||||
| 132 | } | ||||||||||||
| 133 | 19 | return %h; | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 56 | 1; | |||||||||||
| 137 | |||||||||||||
| 138 | =back | ||||||||||||
| 139 | |||||||||||||
| 140 | =head1 BUGS | ||||||||||||
| 141 | |||||||||||||
| 142 | None known. | ||||||||||||
| 143 | |||||||||||||
| 144 | Please report any bugs you find via the CPAN RT system. | ||||||||||||
| 145 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||
| 146 | |||||||||||||
| 147 | =head1 AUTHOR | ||||||||||||
| 148 | |||||||||||||
| 149 | Copyright Fotango 2005. All rights reserved. | ||||||||||||
| 150 | |||||||||||||
| 151 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||
| 152 | on this project. | ||||||||||||
| 153 | |||||||||||||
| 154 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||
| 155 | the same terms as Perl itself. | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 SEE ALSO | ||||||||||||
| 158 | |||||||||||||
| 159 | L<Froody> | ||||||||||||
| 160 | |||||||||||||
| 161 | =cut | ||||||||||||
| 162 | |||||||||||||
| 163 | 56 | 1; | |||||||||||