Created
October 15, 2009 16:26
-
-
Save gardejo/211070 to your computer and use it in GitHub Desktop.
sample code snippet to explain plurally dependeded attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/perl | |
# sample code snippet to explain plurally dependeded attributes | |
# see http://blog.eorzea.asia/2009/10/post_72.html | |
use strict; | |
use warnings; | |
use utf8; | |
{ | |
package MyApp::Addressee; | |
# use Moose; | |
# use MooseX::Types; | |
# use Moose::Util::TypeConstraints; | |
use Any::Moose; | |
use Any::Moose 'X::Types'; | |
use Any::Moose 'Util::TypeConstraints'; | |
use namespace::clean -except => [qw(meta)]; | |
# 1ファイルに書く場合use MyApp::Typesが使えないので、便宜的に匿名制約にする | |
my $title_constraint = enum [qw(Mr. Ms. Miss Mrs. Dr.)]; | |
has 'name' => ( | |
is => 'rw', | |
isa => 'Str', | |
trigger => sub { | |
$_[0]->_clear_addressee; | |
}, | |
); | |
has 'title' => ( | |
is => 'rw', | |
isa => $title_constraint, | |
trigger => sub { | |
$_[0]->_clear_addressee; | |
}, | |
); | |
has 'addressee' => ( | |
is => 'ro', | |
isa => 'Str', | |
lazy_build => 1, | |
clearer => '_clear_addressee', | |
); | |
sub _build_addressee { | |
$_[0]->title . q{ } . $_[0]->name; | |
} | |
__PACKAGE__->meta->make_immutable; | |
} | |
{ | |
use Encode; | |
use Test::More; | |
my $addressee = MyApp::Addressee->new( | |
title => 'Mr.', | |
name => 'John Doe', | |
); | |
is $addressee->addressee, 'Mr. John Doe' | |
=> encode_utf8('ジョン=ドゥ様'); | |
$addressee->title('Dr.'); | |
is $addressee->addressee, 'Dr. John Doe' | |
=> encode_utf8('ジョン=ドゥ博士'); | |
done_testing; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment