Skip to content

Commit

Permalink
Make config a class attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Feb 4, 2018
1 parent d47e339 commit f45634d
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions Lotto.pm
Expand Up @@ -5,6 +5,29 @@ use warnings;
use 5.010;

use Moose;
use MooseX::ClassAttribute;

class_has config => (
isa => 'HashRef',
is => 'ro',
lazy_build => 1,
);

sub _build_config {
my $config;

while (<Lotto::DATA>) {
chomp;
my @conf = split /:/;
my $key = shift @conf;
foreach my $def (@conf) {
my ($count, $limit) = split /x/, $def;
push @{$config->{$key}}, { limit => $limit, count => $count };
}
}

return $config;
}

has type => (
isa => 'Str',
Expand All @@ -16,12 +39,10 @@ has count => (
is => 'ro',
);

our $config;

sub play {
my $self = shift;

my $lotto = $config->{$self->type};
my $lotto = $self->config->{$self->type};

my @results;

Expand All @@ -41,22 +62,12 @@ sub play {
return @results;
}

sub parse_config {
while (<DATA>) {
chomp;
my @conf = split /:/;
my $key = shift @conf;
foreach my $def (@conf) {
my ($count, $limit) = split /x/, $def;
push @{$config->{$key}}, { limit => $limit, count => $count };
}
}
}

around BUILDARGS => sub {
my $orig = shift;
my $class = shift;

my $config = $class->config;

if (@_ == 1 and ref $_[0] eq 'HASH') {
return $class->$orig(@_);
}
Expand All @@ -65,8 +76,6 @@ around BUILDARGS => sub {
return $class->$orig(@_);
}

parse_config() unless keys %$config;

my ($type, $count) = qw[lotto 1];
my @errs;

Expand All @@ -88,7 +97,7 @@ around BUILDARGS => sub {
if (/^\d+$/) {
$count = $_;
}
if (exists $config->{$_}) {
if ($config->{$_}) {
$type = $_;
}
}
Expand All @@ -102,7 +111,7 @@ around BUILDARGS => sub {

if (@_ || @errs) {
push @errs, 'Usage: lotto [' .
join('|', keys %$config) .
join('|', keys %{$class->config}) .
"] [count]\n";
die join "\n", @errs;
}
Expand Down

0 comments on commit f45634d

Please sign in to comment.