1 package # hide from PAUSE
10 use Scalar::Util qw(blessed looks_like_number reftype);
11 use Class::Load qw(load_optional_class);
14 my ($package, @methods) = @_;
16 for my $method (@methods) {
17 my $check = $package->can($method) or confess "$package does not export $method";
18 my $coerce = $package->can("coerce_$method");
19 my $full_method = "${caller}::${method}";
21 *{$full_method} = subname $full_method => sub {
23 ($coerce && $args{coerce} && wantarray)
24 ? ( $check, coerce => $coerce )
32 my ($default, $value, %args) = @_;
33 if(my $err = $args{err}) {
34 confess $err->($value);
41 error("Value $_[0] must be a string")
42 unless Defined(@_) && !ref $_[0];
46 error("Value $_[0] must be a Path::Class::Dir")
47 unless Object(@_) && $_[0]->isa("Path::Class::Dir");
50 sub coerce_Dir{ dir($_[0]) }
53 error("Value $_[0] must be a Path::Class::File")
54 unless Object(@_) && $_[0]->isa("Path::Class::File");
57 sub coerce_File { file($_[0]) }
60 error("Value must be Defined", @_)
61 unless defined($_[0]);
65 error("Value must be UnDefined", @_)
66 unless !defined($_[0]);
70 error("$_[0] is not a valid Boolean", @_)
71 unless(!defined($_[0]) || $_[0] eq "" || "$_[0]" eq '1' || "$_[0]" eq '0');
75 error("weight must be a Number greater than or equal to 0, not $_[0]", @_)
76 unless(Defined(@_) && looks_like_number($_[0]));
80 error("$_[0] must be an Integer", @_)
81 unless(Number(@_) && (int($_[0]) == $_[0]));
85 error("$_[0] must be a HashRef", @_)
86 unless(Defined(@_) && (reftype($_[0]) eq 'HASH'));
90 error("$_[0] must be an ArrayRef", @_)
91 unless(Defined(@_) && (reftype($_[0]) eq 'ARRAY'));
97 JSON::Any->import(qw(DWIW XS JSON));
98 my $json = JSON::Any->new(allow_barekey => 1, allow_singlequote => 1, relaxed=>1);
99 my $ret = $json->jsonToObj($json_str);
107 sub coerce_DBICHashRef {
108 !ref $_[0] ? _json_to_data(@_)
109 : reftype $_[0] eq 'HASH' ? $_[0]
110 : error("Cannot coerce @{[reftype $_[0]]}")
114 sub DBICConnectInfo {
118 sub coerce_DBICConnectInfo {
119 !ref $_[0] ? _json_to_data(@_)
120 : reftype $_[0] eq 'ARRAY' ? $_[0]
121 : reftype $_[0] eq 'HASH' ? [ $_[0] ]
122 : error("Cannot coerce @{[reftype $_[0]]}")
127 error("value must be a Number greater than or equal to 0, not $_[0]", @_)
128 unless(Number(@_) && ($_[0] >= 0));
131 sub PositiveInteger {
132 error("Value must be a Number greater than or equal to 0, not $_[0]", @_)
133 unless(Integer(@_) && ($_[0] >= 0));
137 error("$_[0] is not a loadable Class", @_)
138 unless(load_optional_class($_[0]));
142 error("Value is not an Object", @_)
143 unless(Defined(@_) && blessed($_[0]));
147 error("Need an Object of type DBIx::Class::Storage::DBI, not ".ref($_[0]), @_)
148 unless(Object(@_) && ($_[0]->isa('DBIx::Class::Storage::DBI')));
151 sub DBICStorageDBIReplicatedPool {
152 error("Need an Object of type DBIx::Class::Storage::DBI::Replicated::Pool, not ".ref($_[0]), @_)
153 unless(Object(@_) && ($_[0]->isa('DBIx::Class::Storage::DBI::Replicated::Pool')));
157 error("Need an Object of type DBIx::Class::Schema, not ".ref($_[0]), @_)
158 unless(Object(@_) && ($_[0]->isa('DBIx::Class::Schema')));
161 sub DBICSchemaClass {
162 error("Need an Object of type DBIx::Class::Schema, not ".ref($_[0]), @_)
163 unless(LoadableClass(@_) && ($_[0]->isa('DBIx::Class::Schema')));
166 sub DoesDBICStorageReplicatedBalancer {
167 error("$_[0] does not do DBIx::Class::Storage::DBI::Replicated::Balancer", @_)
168 unless(Object(@_) && $_[0]->does('DBIx::Class::Storage::DBI::Replicated::Balancer') );