X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FUtils.pm;h=fb251877d34c4dc8d0ca094ba22c1db9987b2313;hb=dbe5c90463dd1b323513739b1d27607186caddac;hp=22a21943f2326471ff648f7f31681b845e5d798d;hpb=0f21885a9a4e44bb0d4574617cee75db076a4306;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/Utils.pm b/lib/DBIx/Class/Schema/Loader/Utils.pm index 22a2194..fb25187 100644 --- a/lib/DBIx/Class/Schema/Loader/Utils.pm +++ b/lib/DBIx/Class/Schema/Loader/Utils.pm @@ -3,23 +3,36 @@ package # hide from PAUSE use strict; use warnings; -use Data::Dumper (); use Test::More; +use String::CamelCase 'wordsplit'; +use Carp::Clan qw/^DBIx::Class/; +use List::Util 'all'; +use Data::Dumper (); +use base 'Exporter'; use namespace::clean; -use Exporter 'import'; -our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path warnings_exist warnings_exist_silent/; -use constant BY_CASE_TRANSITION => +our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path no_warnings warnings_exist warnings_exist_silent slurp_file write_file array_eq sigwarn_silencer apply firstidx uniq/; + +use constant BY_CASE_TRANSITION_V7 => qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/; use constant BY_NON_ALPHANUM => qr/[\W_]+/; -sub split_name($) { - my $name = shift; +my $LF = "\x0a"; +my $CRLF = "\x0d\x0a"; + +sub split_name($;$) { + my ($name, $v) = @_; + + my $is_camel_case = $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/; + + if ((not $v) || $v >= 8) { + return map split(BY_NON_ALPHANUM, $_), wordsplit($name); + } - split $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/ ? BY_CASE_TRANSITION : BY_NON_ALPHANUM, $name; + return split $is_camel_case ? BY_CASE_TRANSITION_V7 : BY_NON_ALPHANUM, $name; } sub dumper($) { @@ -38,15 +51,42 @@ sub dumper_squashed($) { return $dd->Values([ $val ])->Dump; } +# copied from DBIx::Class::_Util, import from there once it's released +sub sigwarn_silencer { + my $pattern = shift; + + croak "Expecting a regexp" if ref $pattern ne 'Regexp'; + + my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) }; + + return sub { &$orig_sig_warn unless $_[0] =~ $pattern }; +} + +# Copied with stylistic adjustments from List::MoreUtils::PP +sub firstidx (&@) { + my $f = shift; + foreach my $i (0..$#_) { + local *_ = \$_[$i]; + return $i if $f->(); + } + return -1; +} + +sub uniq (@) { + my %seen = (); + grep { not $seen{$_}++ } @_; +} + +sub apply (&@) { + my $action = shift; + $action->() foreach my @values = @_; + wantarray ? @values : $values[-1]; +} + sub eval_package_without_redefine_warnings { my ($pkg, $code) = @_; - my $warn_handler = $SIG{__WARN__} || sub { warn @_ }; - - local $SIG{__WARN__} = sub { - $warn_handler->(@_) - unless $_[0] =~ /^Subroutine \S+ redefined/; - }; + local $SIG{__WARN__} = sigwarn_silencer(qr/^Subroutine \S+ redefined/); # This hairiness is to handle people using "use warnings FATAL => 'all';" # in their custom or external content. @@ -84,6 +124,22 @@ sub class_path { return $class_path; } +sub no_warnings(&;$) { + my ($code, $test_name) = @_; + + my $failed = 0; + + my $warn_handler = $SIG{__WARN__} || sub { warn @_ }; + local $SIG{__WARN__} = sub { + $failed = 1; + $warn_handler->(@_); + }; + + $code->(); + + ok ((not $failed), $test_name); +} + sub warnings_exist(&$$) { my ($code, $re, $test_name) = @_; @@ -116,6 +172,37 @@ sub warnings_exist_silent(&$$) { ok $matched, $test_name; } +sub slurp_file($) { + my $file_name = shift; + + open my $fh, '<:encoding(UTF-8)', $file_name, + or croak "Can't open '$file_name' for reading: $!"; + + my $data = do { local $/; <$fh> }; + + close $fh; + + $data =~ s/$CRLF|$LF/\n/g; + + return $data; +} + +sub write_file($$) { + my $file_name = shift; + + open my $fh, '>:encoding(UTF-8)', $file_name, + or croak "Can't open '$file_name' for writing: $!"; + + print $fh shift; + close $fh; +} + +sub array_eq($$) { + no warnings 'uninitialized'; + my ($l, $r) = @_; + + return @$l == @$r && all { $l->[$_] eq $r->[$_] } 0..$#$l; +} 1; # vim:et sts=4 sw=4 tw=0: