BUILDARGS.
Revision history for Perl extension MooseX-Singleton
+0.15
+ - When an object was implicitly constructed by calling
+ ClassName->attribute, it skipped the BUILD and BUILDARGS defined
+ for a class. Report and test from Josh in RT #42690. (Dave
+ Rolsky)
+
0.14 2009-01-22
- Converted to use new method generation helpers in the most
recent Moose (0.65) and Class::MOP (Dave Rolsky)
no strict 'refs';
return ${"$instance\::singleton"} if defined ${"$instance\::singleton"};
- return $instance->meta->construct_instance;
+ # We need to go through ->new in order to make sure BUILD and
+ # BUILDARGS get called.
+ return $instance->meta->name->new;
}
sub clone_instance {
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+{
+ package MySingleton;
+ use MooseX::Singleton;
+
+ has 'attrib' =>
+ is => 'rw',
+ isa => 'Str',
+ default => 'foo';
+
+ sub hello {'world'}
+
+ sub BUILDARGS {
+ my ( $class, %opts ) = @_;
+
+ { attrib => 'bar', %opts };
+ }
+}
+
+is(
+ MySingleton->attrib, 'bar',
+ 'BUILDARGS changed value of attrib when instance was auto-instantiated'
+);
+
+MySingleton->meta->remove_package_glob('singleton');
+
+MySingleton->instance;
+
+is(
+ MySingleton->attrib, 'bar',
+ 'BUILDARGS changed value of attrib when instance was explicitly instantiated'
+);
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+{
+ package MySingleton;
+ use MooseX::Singleton;
+
+ has 'attrib' =>
+ is => 'rw',
+ isa => 'Str',
+ default => 'foo';
+
+ sub hello {'world'}
+
+ sub BUILDARGS {
+ my ( $class, %opts ) = @_;
+
+ { attrib => 'bar', %opts };
+ }
+
+ __PACKAGE__->meta->make_immutable;
+}
+
+is(
+ MySingleton->attrib, 'bar',
+ 'BUILDARGS changed value of attrib when instance was auto-instantiated'
+);
+
+MySingleton->meta->remove_package_glob('singleton');
+
+MySingleton->instance;
+
+is(
+ MySingleton->attrib, 'bar',
+ 'BUILDARGS changed value of attrib when instance was explicitly instantiated'
+);