Object-Container-Exporter

 view release on metacpan or  search on metacpan

lib/Object/Container/Exporter.pm  view on Meta::CPAN

        |-- Api
        |   `-- Password.pm
        `-- Container.pm

    #your sub class
    package MyApp::Container;
    use Object::Container::Exporter -base;

    #your main script
    use MyApp::Container qw/api/;
    
    my $hash_val = api('Password')->generate($pass);

When you wanna export shortcut function to call the class object in any directory, you can register your original shortcut function in the sub class.

Examples are:

    #your application tree
    `-- MyApp
        |-- Model
        |   |-- Api
        |   |   `-- User.pm
        |   `-- Command
        |       `-- Password.pm
        `-- Container.pm

    #your sub class
    package MyApp::Container;
    use Object::Container::Exporter -base;

    register_namespace cmd => 'Mock::Model::Api::Command';

    #your main script
    use MyApp::Container qw/api cmd/;
    
    my $hash_val = cmd('Password')->generate($pass);

    my $row = api('User')->register(
        id   => 'nekokak',
        pass => $hash_val,
    );

Now, you have efficiently fun life development.

=head1 METHODS

=head2 register

Register classes to container.

Examples are:

    package MyApp::Container;
    use Object::Container::Exporter -base;

    #register($register_name, $initializer_code);
    register db => sub {
        my $self = shift;
        $self->load_class('DBI');
        DBI->connect('dbi:mysql:sandbox', 'root', 'pass', +{
            mysql_enable_utf8 => 1,
            PrintError        => 0,
            RaiseError        => 1,
        },);
    };

    #register($load_class,@opts);
    register 'WWW::Mechanize', @args;

=head2 register_namespace

You can register your original function name to call your application calss objects.

Example is:

    package MyApp::Container;
    use Object::Container::Exporter -base;

    register_namespace form => 'MyApp::Api::Form';

=head2 register_default_container_name

To call the object registered your sub class, the 'container' function exported. But you can change the export function name.

Example is:

    #your sub class
    package MyApp::Container;
    use Object::Container::Exporter -base;

    register_default_container_name 'con';

    register db => sub {
        my $self = shift;
        $self->load_class('DBI');
        DBI->connect('dbi:mysql:sandbox', 'root', 'pass',);
    };

    #your main script
    use MyApp::Container;
    
    my $user_bodys = con('db')->selectcolcall_arrayref('SELECT body FROM user');

=head2 get

Get the object that registered by 'register' method.

Examples are:
    #your sub class
    package MyApp::Container;
    use Object::Container::Exporter -base;

    register dbh => sub {
        my $self = shift;
        $self->load_class('DBI');
        DBI->connect('dbi:mysql:sandbox', 'root', 'pass',);
    };

    register teng => sub {
        my $self = shift;
        $self->load_class('MyApp::DB');



( run in 0.925 second using v1.01-cache-2.11-cpan-5735350b133 )