From: Guillermo Roditi Date: Mon, 29 Oct 2007 20:46:34 +0000 (+0000) Subject: changes to lazy_build naming conventions X-Git-Tag: 0_27~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=58f85113419d7bbb2488f4b012b3355a26777cd5;p=gitmo%2FMoose.git changes to lazy_build naming conventions --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 1c64d20..8a73555 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -179,12 +179,11 @@ sub _process_options { if exists $options->{default}; $options->{lazy} = 1; $options->{required} = 1; + $options->{builder} ||= "_build_${name}"; if($name =~ /^_/){ - $options->{builder} ||= "_build${name}"; $options->{clearer} ||= "_clear${name}"; $options->{predicate} ||= "_has${name}"; } else { - $options->{builder} ||= "build_${name}"; $options->{clearer} ||= "clear_${name}"; $options->{predicate} ||= "has_${name}"; } @@ -609,17 +608,23 @@ and predicate options for you using the following convention. #If your attribute name starts with an underscore: has '_foo' => (lazy_build => 1); #is the same as - has '_foo' => (lazy => 1, required => 1, builder => '_build_foo', predicate => '_has_foo', clearer => '_clear_foo'); + has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', builder => '_build__foo); # or - has '_foo' => (lazy => 1, required => 1, builder => '_build_foo', default => sub{shift->_build_foo}, clearer => '_clear_foo'); + has '_foo' => (lazy => 1, required => 1, predicate => '_has_foo', clearer => '_clear_foo', default => sub{shift->_build__foo}); #If your attribute name does not start with an underscore: - has 'foo' => (lazy => 1, required => 1, builder => 'build_foo', predicate => 'has_foo', clearer => 'clear_foo'); + has 'foo' => (lazy_build => 1); + #is the same as + has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', builder => '_build_foo); # or - has '_foo' => (lazy => 1, required => 1, builder => '_build_foo', default => sub{shift->build_foo}, clearer => '_clear_foo'); + has 'foo' => (lazy => 1, required => 1, predicate => 'has_foo', clearer => 'clear_foo', default => sub{shift->_build_foo}); + +The reason for the different naming of the C is that the C +method is a private method while the C and C methods +are public methods. NOTE: This means your class should provide a method whose name matches the value -of the builder part, in this case _build_foo or build_foo. +of the builder part, in this case _build__foo or _build_foo. =item B diff --git a/t/020_attributes/012_misc_attribute_tests.t b/t/020_attributes/012_misc_attribute_tests.t index ead2979..d7be475 100644 --- a/t/020_attributes/012_misc_attribute_tests.t +++ b/t/020_attributes/012_misc_attribute_tests.t @@ -171,8 +171,8 @@ BEGIN { has 'foo' => ( lazy_build => 1, is => 'ro'); has '_foo' => ( lazy_build => 1, is => 'ro'); - sub build_foo { return "works" }; - sub _build_foo { return "works too" }; + sub _build_foo { return "works" }; + sub _build__foo { return "works too" }; } my $meta = Test::LazyBuild::Attribute->meta; @@ -187,7 +187,7 @@ BEGIN { is($foo_attr->clearer, "clear_foo", ".. and it's named clear_foo"); ok($foo_attr->has_builder, "foo has builder"); - is($foo_attr->builder, "build_foo", ".. and it's named build_foo"); + is($foo_attr->builder, "_build_foo", ".. and it's named build_foo"); ok($foo_attr->has_predicate, "foo has predicate"); is($foo_attr->predicate, "has_foo", ".. and it's named has_foo"); @@ -200,7 +200,7 @@ BEGIN { is($_foo_attr->clearer, "_clear_foo", ".. and it's named _clear_foo"); ok($_foo_attr->has_builder, "_foo has builder"); - is($_foo_attr->builder, "_build_foo", ".. and it's named _build_foo"); + is($_foo_attr->builder, "_build__foo", ".. and it's named _build_foo"); ok($_foo_attr->has_predicate, "_foo has predicate"); is($_foo_attr->predicate, "_has_foo", ".. and it's named _has_foo");