Skip to content

Instantly share code, notes, and snippets.

@three18ti
Created January 27, 2014 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save three18ti/8649602 to your computer and use it in GitHub Desktop.
Save three18ti/8649602 to your computer and use it in GitHub Desktop.
package My::App;
use 5.010;
use Moose;
use strict;
use warnings;
use MooseX::HasDefaults::RO;
has [qw( foo bar )] => (
isa => 'Int',
required => 1,
);
has [qw( biz baz )] => (
isa => 'Int',
default => '1',
);
package My;
use 5.010;
use Moose;
use strict;
use warnings;
use MooseX::HasDefaults::RO;
has [qw(app_foo app_bar)] => (
isa => 'Int',
required => 1,
);
has [qw(app_biz app_baz)] => (
isa => 'Int',
required => 0,
);
has 'MyApp' => (
lazy => 1,
isa => 'My::App',
default => sub {
my $attrs = [qw(
biz baz
)];
my %opt_params;
foreach my $attribute (@$attrs) {
my $method_name = 'app_' . $attribute;
$opt_params{$attribute} = $_[0]->$method_name if $_[0]->$method_name;
}
My::App->new(
foo => $_[0]->app_foo,
bar => $_[0]->app_bar,
%opt_params,
);
},
handles => qr/^(.*)/,
);
package main;
use 5.010;
use strict;
use warnings;
use Test::More;
#BEGIN { use_ok 'My' }
my $app = new_ok 'My' => [
app_foo => 1,
app_bar => 2,
app_biz => 3,
];
foreach my $test (qw( foo bar biz baz app_foo app_bar app_biz app_baz )){
is $app->$test, $app->$test, '$app' . "->" . $test;
}
is $app->foo, 1, "foo ok";
is $app->app_foo, 1, "app_foo ok";
my $count = 1;
foreach my $test (qw( foo bar biz baz app_foo app_bar app_biz app_baz )){
is $app->$test, $count %4 ? $app->$test : undef , '$app' . "->" . $test;
}
done_testing;
cat: o: No such file or directory
root@kitt:~/Build-VM# nano test_moose.pl
root@kitt:~/Build-VM# cat test_moose.pl
package My::App;
use 5.010;
use Moose;
use strict;
use warnings;
use MooseX::HasDefaults::RO;
has [qw( foo bar )] => (
isa => 'Int',
required => 1,
);
has [qw( biz baz )] => (
isa => 'Int',
default => '1',
);
package My;
use 5.010;
use Moose;
use strict;
use warnings;
use MooseX::HasDefaults::RO;
has [qw(app_foo app_bar)] => (
isa => 'Int',
required => 1,
);
has [qw(app_biz app_baz)] => (
isa => 'Int',
required => 0,
);
has 'MyApp' => (
lazy => 1,
isa => 'My::App',
default => sub {
my $attrs = [qw(
biz baz
)];
my %opt_params;
foreach my $attribute (@$attrs) {
my $method_name = 'app_' . $attribute;
$opt_params{$attribute} = $_[0]->$method_name if $_[0]->$method_name;
}
My::App->new(
foo => $_[0]->app_foo,
bar => $_[0]->app_bar,
%opt_params,
);
},
handles => qr/^(.*)/,
);
package main;
use 5.010;
use strict;
use warnings;
use Test::More;
#BEGIN { use_ok 'My' }
my $app = new_ok 'My' => [
app_foo => 1,
app_bar => 2,
app_biz => 3,
];
foreach my $test (qw( foo bar biz baz app_foo app_bar app_biz app_baz )){
is $app->$test, $app->$test, '$app' . "->" . $test;
}
is $app->foo, 1, "foo ok";
is $app->app_foo, 1, "app_foo ok";
my $count = 1;
foreach my $test (qw( foo bar biz baz app_foo app_bar app_biz app_baz )){
is $app->$test, $count %4 ? $app->$test : undef , '$app' . "->" . $test;
}
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment