[carton] 248/472: Convert basic objects to Moo

Lucas Kanashiro kanashiro-guest at moszumanska.debian.org
Fri Jul 24 00:38:51 UTC 2015


This is an automated email from the git hooks/post-receive script.

kanashiro-guest pushed a commit to branch master
in repository carton.

commit a7e8dad620a48708f84d021c24be412c00c2b6fb
Author: Tatsuhiko Miyagawa <miyagawa at bulknews.net>
Date:   Sat Jun 1 17:45:28 2013 +0900

    Convert basic objects to Moo
---
 cpanfile                 |  1 +
 lib/Carton/CLI.pm        |  4 ++--
 lib/Carton/Dependency.pm | 13 +++++++++++++
 lib/Carton/Index.pm      | 12 +++++-------
 lib/Carton/Lock.pm       | 19 ++++++++++---------
 lib/Carton/Package.pm    | 19 ++++++++-----------
 6 files changed, 39 insertions(+), 29 deletions(-)

diff --git a/cpanfile b/cpanfile
index 8e971d3..21398a3 100644
--- a/cpanfile
+++ b/cpanfile
@@ -10,6 +10,7 @@ requires 'local::lib', 1.008;
 requires 'Exception::Class', 1.32;
 requires 'Getopt::Long', 2.39;
 requires 'Module::CPANfile', 0.9031;
+requires 'Moo', '1.002';
 
 # MYMETA support
 requires 'App::cpanminus', 1.6915;
diff --git a/lib/Carton/CLI.pm b/lib/Carton/CLI.pm
index 6642719..fd0cb6f 100644
--- a/lib/Carton/CLI.pm
+++ b/lib/Carton/CLI.pm
@@ -204,8 +204,8 @@ sub cmd_list {
     my $lock = $self->find_lock
         or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n");
 
-    for my $module ($lock->modules) {
-        $self->print("$module->{dist}\n");
+    for my $dependency ($lock->dependencies) {
+        $self->print($dependency->distname . "\n");
     }
 }
 
diff --git a/lib/Carton/Dependency.pm b/lib/Carton/Dependency.pm
new file mode 100644
index 0000000..65d0347
--- /dev/null
+++ b/lib/Carton/Dependency.pm
@@ -0,0 +1,13 @@
+package Carton::Dependency;
+use strict;
+use CPAN::Meta;
+use Moo;
+
+has meta => (is => 'ro', coerce => sub { CPAN::Meta->new($_[0], { lazy_validation => 1 }) });
+
+sub distname {
+    my $self = shift;
+    sprintf '%s-%s', $self->meta->name, $self->meta->version;
+}
+
+1;
diff --git a/lib/Carton/Index.pm b/lib/Carton/Index.pm
index 456e2b1..328691d 100644
--- a/lib/Carton/Index.pm
+++ b/lib/Carton/Index.pm
@@ -1,24 +1,22 @@
 package Carton::Index;
 use strict;
+use Moo;
 
-sub new {
-    my($class, $packages) = @_;
-    bless { packages => {} }, $class;
-}
+has _packages => (is => 'rw', default => sub { +{} });
 
 sub add_package {
     my($self, $package) = @_;
-    $self->{packages}{$package->name} = $package; # XXX ||=
+    $self->_packages->{$package->name} = $package; # XXX ||=
 }
 
 sub count {
     my $self = shift;
-    scalar keys %{$self->{packages}};
+    scalar keys %{$self->_packages};
 }
 
 sub packages {
     my $self = shift;
-    sort { $a->name cmp $b->name } values %{$self->{packages}};
+    sort { $a->name cmp $b->name } values %{$self->_packages};
 }
 
 sub write {
diff --git a/lib/Carton/Lock.pm b/lib/Carton/Lock.pm
index 2be664a..3b367b6 100644
--- a/lib/Carton/Lock.pm
+++ b/lib/Carton/Lock.pm
@@ -1,8 +1,13 @@
 package Carton::Lock;
 use strict;
+use Carton::Dependency;
 use Carton::Package;
 use Carton::Index;
 use Carton::Util;
+use Moo;
+
+has version => (is => 'ro');
+has modules => (is => 'ro', default => sub { +{} });
 
 sub from_file {
     my($class, $file) = @_;
@@ -11,23 +16,19 @@ sub from_file {
     return $class->new($data);
 }
 
-sub new {
-    my($class, $data) = @_;
-    bless $data, $class;
-}
-
 sub write {
     my($self, $file) = @_;
     Carton::Util::dump_json({ %$self }, $file);
 }
 
-sub modules {
-    values %{$_[0]->{modules} || {}};
+sub dependencies {
+    map Carton::Dependency->new(meta => $_->{mymeta}),
+      values %{$_[0]->modules}
 }
 
 sub find {
     my($self, $module) = @_;
-    $self->{modules}{$module};
+    $self->modules->{$module};
 }
 
 sub index {
@@ -45,7 +46,7 @@ sub packages {
     my $self = shift;
 
     my @packages;
-    while (my($name, $metadata) = each %{$self->{modules}}) {
+    while (my($name, $metadata) = each %{$self->modules}) {
         while (my($package, $provides) = each %{$metadata->{provides}}) {
             # TODO what if duplicates?
             push @packages, Carton::Package->new($package, $provides->{version}, $metadata->{pathname});
diff --git a/lib/Carton/Package.pm b/lib/Carton/Package.pm
index cfa2b54..71ed549 100644
--- a/lib/Carton/Package.pm
+++ b/lib/Carton/Package.pm
@@ -1,18 +1,15 @@
 package Carton::Package;
 use strict;
+use Moo;
 
-sub new {
-    my($class, $name, $version, $pathname) = @_;
-    bless {
-        name => $name,
-        version => $version,
-        pathname => $pathname,
-    }, $class;
-}
+has name     => (is => 'ro');
+has version  => (is => 'ro');
+has pathname => (is => 'ro');
 
-sub name     { $_[0]->{name} }
-sub version  { $_[0]->{version} }
-sub pathname { $_[0]->{pathname} }
+sub BUILDARGS {
+    my($class, @args) = @_;
+    return { name => $args[0], version => $args[1], pathname => $args[2] };
+}
 
 1;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/carton.git



More information about the Pkg-perl-cvs-commits mailing list