Skip to content

Commit

Permalink
Moves custom Moose types into MetaCPAN::Types::Internal.
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed Oct 11, 2014
1 parent 743b412 commit a59eae7
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 146 deletions.
1 change: 1 addition & 0 deletions cpanfile
Expand Up @@ -97,6 +97,7 @@ requires 'MooseX::Types::ElasticSearch', ' == 0.0.2'; # Newer versions use the o
requires 'MooseX::Types::Moose';
requires 'MooseX::Types::Path::Class';
requires 'MooseX::Types::Structured';
requires 'MooseX::Types::URI';
requires 'Mozilla::CA';
requires 'Net::Twitter';
requires 'Parse::CPAN::Packages::Fast', '0.04';
Expand Down
155 changes: 9 additions & 146 deletions lib/MetaCPAN/Types.pm
Expand Up @@ -3,154 +3,17 @@ package MetaCPAN::Types;
use strict;
use warnings;

use CPAN::Meta;
use ElasticSearch;
use ElasticSearchX::Model::Document::Types qw(:all);
use JSON;
use MooseX::Getopt::OptionTypeMap;
use MooseX::Types::Common::String qw(NonEmptySimpleStr);
use MooseX::Types::Moose qw( ArrayRef Bool HashRef Item Int Num Str Undef );
use MooseX::Types::Structured qw(Dict Tuple Optional);
use parent 'MooseX::Types::Combine';

use MooseX::Types -declare => [
__PACKAGE__->provide_types_from(
qw(
Logger
Resources
Stat
Module
AssociatedPod
Identity
Dependency
Extra
Profile
Blog
PerlMongers
Tests
BugSummary
MooseX::Types::Common::Numeric
MooseX::Types::Common::String
MooseX::Types::Moose
MooseX::Types::Structured
MooseX::Types::URI
MetaCPAN::Types::Internal
)
];

subtype PerlMongers,
as ArrayRef [ Dict [ url => Optional [Str], name => NonEmptySimpleStr ] ];
coerce PerlMongers, from HashRef, via { [$_] };

subtype Blog, as ArrayRef [ Dict [ url => NonEmptySimpleStr, feed => Str ] ];
coerce Blog, from HashRef, via { [$_] };

subtype Stat,
as Dict [
mode => Int,
uid => Int,
gid => Int,
size => Int,
mtime => Int
];

subtype Module, as ArrayRef [ Type ['MetaCPAN::Document::Module'] ];
coerce Module, from ArrayRef, via {
[ map { ref $_ eq 'HASH' ? MetaCPAN::Document::Module->new($_) : $_ }
@$_ ];
};
coerce Module, from HashRef, via { [ MetaCPAN::Document::Module->new($_) ] };

subtype Identity, as ArrayRef [ Type ['MetaCPAN::Model::User::Identity'] ];
coerce Identity, from ArrayRef, via {
[
map {
ref $_ eq 'HASH'
? MetaCPAN::Model::User::Identity->new($_)
: $_
} @$_
];
};
coerce Identity, from HashRef,
via { [ MetaCPAN::Model::User::Identity->new($_) ] };

subtype Dependency, as ArrayRef [ Type ['MetaCPAN::Document::Dependency'] ];
coerce Dependency, from ArrayRef, via {
[
map {
ref $_ eq 'HASH'
? MetaCPAN::Document::Dependency->new($_)
: $_
} @$_
];
};
coerce Dependency, from HashRef,
via { [ MetaCPAN::Document::Dependency->new($_) ] };

subtype Profile, as ArrayRef [ Type ['MetaCPAN::Document::Author::Profile'] ];
coerce Profile, from ArrayRef, via {
[
map {
ref $_ eq 'HASH'
? MetaCPAN::Document::Author::Profile->new($_)
: $_
} @$_
];
};
coerce Profile, from HashRef,
via { [ MetaCPAN::Document::Author::Profile->new($_) ] };

subtype Tests,
as Dict [ fail => Int, na => Int, pass => Int, unknown => Int ];

subtype BugSummary,
as Dict [
(
map { $_ => Optional [Int] }
qw(new open stalled patched resolved rejected active closed)
),
type => Str,
source => Str
];

subtype Resources,
as Dict [
license => Optional [ ArrayRef [Str] ],
homepage => Optional [Str],
bugtracker =>
Optional [ Dict [ web => Optional [Str], mailto => Optional [Str] ] ],
repository => Optional [
Dict [
url => Optional [Str],
web => Optional [Str],
type => Optional [Str]
]
]
];

coerce Resources, from HashRef, via {
my $r = $_;
return {
map { $_ => $r->{$_} }
grep { defined $r->{$_} }
qw(license homepage bugtracker repository)
};
};

class_type 'CPAN::Meta';
coerce HashRef, from 'CPAN::Meta', via {
my $struct = eval { $_->as_struct( { version => 2 } ); };
return $struct ? $struct : $_->as_struct;
};

class_type Logger, { class => 'Log::Log4perl::Logger' };
coerce Logger, from ArrayRef, via {
return MetaCPAN::Role::Common::_build_logger($_);
};

MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
'MooseX::Types::ElasticSearch::ES' => '=s' );

subtype AssociatedPod, as Item;

use MooseX::Attribute::Deflator;
deflate 'ScalarRef', via {$$_};
inflate 'ScalarRef', via { \$_ };

deflate AssociatedPod, via { ref $_ ? $_->full_path : $_ };
no MooseX::Attribute::Deflator;
);

1;
157 changes: 157 additions & 0 deletions lib/MetaCPAN/Types/Internal.pm
@@ -0,0 +1,157 @@
package MetaCPAN::Types::Internal;

use strict;
use warnings;

use CPAN::Meta;
use ElasticSearch;
use ElasticSearchX::Model::Document::Types qw(:all);
use JSON;
use MooseX::Getopt::OptionTypeMap;
use MooseX::Types::Common::String qw(NonEmptySimpleStr);
use MooseX::Types::Moose qw( ArrayRef Bool HashRef Item Int Num Str Undef );
use MooseX::Types::Structured qw(Dict Tuple Optional);

use MooseX::Types -declare => [
qw(
Logger
Resources
Stat
Module
AssociatedPod
Identity
Dependency
Extra
Profile
Blog
PerlMongers
Tests
BugSummary
)
];

subtype PerlMongers,
as ArrayRef [ Dict [ url => Optional [Str], name => NonEmptySimpleStr ] ];
coerce PerlMongers, from HashRef, via { [$_] };

subtype Blog, as ArrayRef [ Dict [ url => NonEmptySimpleStr, feed => Str ] ];
coerce Blog, from HashRef, via { [$_] };

subtype Stat,
as Dict [
mode => Int,
uid => Int,
gid => Int,
size => Int,
mtime => Int
];

subtype Module, as ArrayRef [ Type ['MetaCPAN::Document::Module'] ];
coerce Module, from ArrayRef, via {
[ map { ref $_ eq 'HASH' ? MetaCPAN::Document::Module->new($_) : $_ }
@$_ ];
};
coerce Module, from HashRef, via { [ MetaCPAN::Document::Module->new($_) ] };

subtype Identity, as ArrayRef [ Type ['MetaCPAN::Model::User::Identity'] ];
coerce Identity, from ArrayRef, via {
[
map {
ref $_ eq 'HASH'
? MetaCPAN::Model::User::Identity->new($_)
: $_
} @$_
];
};
coerce Identity, from HashRef,
via { [ MetaCPAN::Model::User::Identity->new($_) ] };

subtype Dependency, as ArrayRef [ Type ['MetaCPAN::Document::Dependency'] ];
coerce Dependency, from ArrayRef, via {
[
map {
ref $_ eq 'HASH'
? MetaCPAN::Document::Dependency->new($_)
: $_
} @$_
];
};
coerce Dependency, from HashRef,
via { [ MetaCPAN::Document::Dependency->new($_) ] };

subtype Profile, as ArrayRef [ Type ['MetaCPAN::Document::Author::Profile'] ];
coerce Profile, from ArrayRef, via {
[
map {
ref $_ eq 'HASH'
? MetaCPAN::Document::Author::Profile->new($_)
: $_
} @$_
];
};
coerce Profile, from HashRef,
via { [ MetaCPAN::Document::Author::Profile->new($_) ] };

subtype Tests,
as Dict [ fail => Int, na => Int, pass => Int, unknown => Int ];

subtype BugSummary,
as Dict [
(
map { $_ => Optional [Int] }
qw(new open stalled patched resolved rejected active closed)
),
type => Str,
source => Str
];

subtype Resources,
as Dict [
license => Optional [ ArrayRef [Str] ],
homepage => Optional [Str],
bugtracker =>
Optional [ Dict [ web => Optional [Str], mailto => Optional [Str] ] ],
repository => Optional [
Dict [
url => Optional [Str],
web => Optional [Str],
type => Optional [Str]
]
]
];

coerce Resources, from HashRef, via {
my $r = $_;
return {
map { $_ => $r->{$_} }
grep { defined $r->{$_} }
qw(license homepage bugtracker repository)
};
};

class_type 'CPAN::Meta';
coerce HashRef, from 'CPAN::Meta', via {
my $struct = eval { $_->as_struct( { version => 2 } ); };
return $struct ? $struct : $_->as_struct;
};

class_type Logger, { class => 'Log::Log4perl::Logger' };
coerce Logger, from ArrayRef, via {
return MetaCPAN::Role::Common::_build_logger($_);
};

MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
'MooseX::Types::ElasticSearch::ES' => '=s' );

subtype AssociatedPod, as Item;

use MooseX::Attribute::Deflator;
deflate 'ScalarRef', via {$$_};
inflate 'ScalarRef', via { \$_ };

deflate AssociatedPod, via { ref $_ ? $_->full_path : $_ };
no MooseX::Attribute::Deflator;


1;

0 comments on commit a59eae7

Please sign in to comment.