Created
November 7, 2010 03:46
-
-
Save aklaswad/665949 to your computer and use it in GitHub Desktop.
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/bin/perl | |
# MT registry cookbook | |
use strict; | |
use warnings; | |
use lib qw( lib extlib ../lib ../extlib); | |
use utf8; | |
use MT; | |
use MT::Component; | |
use Test::More; | |
# create and register components to MT. | |
# ( normally this is automatically done by MT at initialize phase ) | |
my $c1 = MT::Component->new; | |
my $c2 = MT::Component->new; | |
push @MT::Components, ( $c1, $c2 ); | |
$MT::Component{c1} = $c1; | |
$MT::Component{c2} = $c2; | |
=head1 registry method is Setter/Getter | |
You can use $component->registry() method to Set/Get registry values. | |
=head2 Setter | |
If hashref is in args, it is Setter. | |
$component->registry( 'path' => 'to' => 'set' => { | |
things => 'you', | |
wanna => 'set', | |
}); | |
=cut | |
$c1->registry( this => is => { a => 'pen' } ); | |
is_deeply( | |
$c1->{registry}, | |
{ | |
this => { | |
is => { | |
a => 'pen', | |
plugin => $c1, | |
} | |
} | |
}, | |
'Set value.', | |
); | |
=head2 Getter | |
And, if all args were scalar, it is getter. | |
$component->registry( 'path' => 'to' => 'get' ); | |
=cut | |
is( | |
$c1->registry( this => is => 'a' ), | |
'pen', | |
'Get value.', | |
); | |
=head1 Two more getters | |
=head2 MT::registry() | |
MT::registry method can marge hashes of components. | |
=cut | |
$c1->registry( foo => { bar => 1 } ); | |
$c2->registry( foo => { buz => 1 } ); | |
is_deeply( | |
MT->registry('foo'), | |
{ | |
bar => 1, | |
buz => 1, | |
}, | |
'Get merged value.', | |
); | |
=head2 MT::Component::registry() | |
MT::Component::registry was invoked as class method, returns | |
arrayref of each components' value of the path. | |
=cut | |
is_deeply( | |
MT::Component->registry('foo'), | |
[ | |
{ 'bar' => 1 }, | |
{ 'buz' => 1 }, | |
], | |
'Get multiple values from components as arrayref.', | |
); | |
=head1 Localization | |
In registry, the strings registered with the key name ends with 'label' are | |
automatically translated. | |
This means, If you get the hash tree, all '*label' values in the hash can get | |
as closure. if you want to get the translated string, just run it. | |
=cut | |
$c1->registry({ | |
entry_label => 'Entry', | |
foo => { | |
page_label => 'Page', | |
}, | |
}); | |
MT->new; # required for load Core translate lexicons. | |
MT->set_language('ja'); | |
is( | |
$c1->registry('entry_label'), | |
'ブログ記事', | |
'Get translated value', | |
); | |
my $page_label = $c1->registry('foo')->{page_label}; | |
is( | |
ref $page_label, | |
'CODE', | |
'In deep hash, it is closure.', | |
); | |
is( | |
$page_label->(), | |
'ウェブページ', | |
'And we can get translated value from it.', | |
); | |
=head1 Grafts | |
May use some tricks grafting yaml file or subroutins on to other registry for | |
delay loading or using dynamic values. | |
=head2 sub routine reference | |
If you set subroutin reference to the registry, you can explore the return | |
value of subroutin as it's a part of registry. | |
=cut | |
$c1->registry({ | |
secret => sub { | |
return { | |
'of_life' => 2 * 3 * 7, | |
}; | |
}, | |
}); | |
is( | |
$c1->registry( 'secret' => 'of_life' ), | |
42, | |
'Fetch from coderef grafts.', | |
); | |
=head2 MT specified routine name | |
If the string witch starts with doller sign($) and includes double colon(::), | |
MT understands this is name of perl subroutine. | |
Note that this value is *NOT* the name of subroutin name in perl symbol table. | |
the first part is the MT::Component's ID value. | |
$ + COMPONENT_ID + :: + FULL::NAME::OF::SUB | |
=cut | |
sub my_sub { | |
my $component = shift; | |
return { | |
nelson => "histoire" | |
}; | |
} | |
$c1->registry({ melody => '$c1::main::my_sub' }); | |
is( | |
$c1->registry( melody => 'nelson' ), | |
'histoire', | |
'Fetch from subroutine grafts', | |
); | |
=pod | |
or you wanna invoke the routine as class method, you can use another syntax | |
$ + COMPONENT_ID + :: + PACKAGE::NAME->routine_name | |
this means, set I<COMPONENT_ID> to the current component and run I<routine_name> method | |
of I<PACKAGE::NAME> package, then grafts retun value on to the registry. | |
(this needs real file lib/PACKAGE/NAME.pm at somewhere in search path) | |
=cut | |
package MT; | |
sub this_is_me { | |
my $pkg = shift; | |
my $component = shift; | |
return { | |
my_name => "my name is $pkg", | |
} | |
} | |
package main; | |
$c1->registry({ this_is_me => '$c1::MT->this_is_me' }); | |
is( | |
$c1->registry( this_is_me => 'my_name' ), | |
'my name is MT', | |
'Fetch from class method grafts.' | |
); | |
=head2 YAML file | |
Registry getters can read inside of yaml file, if the yaml file name | |
( string ends with '.yaml' ) was found in the value of registry. | |
MT will search the specified yaml file from the component's base path. | |
=cut | |
# sorry this test will be skipped until you put registry-test.yaml | |
# in the same place of this script and that file includes this line. | |
#--- | |
#greeting: Hello from registry-test.yaml. | |
#--- | |
SKIP: { | |
skip "No yaml file for test", 1 unless -f 'registry-test.yaml'; | |
$c1->path('.'); | |
$c1->registry({ yaml => 'registry-test.yaml' }); | |
is( | |
$c1->registry('yaml' => 'greeting'), | |
'Hello from registry-test.yaml.', | |
'Fetch from yaml file grafts.', | |
); | |
} | |
done_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment