Coder Social home page Coder Social logo

catalyst-plugin-mapcomponentdependencies's Introduction

NAME

Catalyst::Plugin::MapComponentDependencies - Allow components to depend on other components

SYNOPSIS

package MyApp;

use Moose;
use Catalyst;

with 'Catalyst::Plugin::MapComponentDependencies';

MyApp->map_dependencies(
  'Model::Foo' => {
    bar => 'Model::Bar',
    baz => sub {
      my ($app_or_ctx, $component_name) = @_;
      return ...;
    },
  },
);

MyApp->config(
  'Model::Foo' => { another_param => 'value' }
)

MyApp->setup;

During setup when 'Model::Foo' is created it will get all three key / value pairs send to ->new.

NOTE: You need to compose this plugin via the Moose 'with' subroutine if you want to get the handy class methods 'map_dependencies' and 'map_dependency'. If you prefer you may setup you dependencies via configuration:

package MyApp;

use Catalyst 'MapComponentDependencies';

MyApp->config(
  'Model::Foo' => { another_param => 'value' },
  'Plugin::MapComponentDependencies' => {
    map_dependencies => {
      'Model::Foo' => {
        bar => 'Model::Bar',
        baz => sub {
          my ($app_or_ctx, $component_name, $from_config_args) = @_;
          return ...;
        },
      },
    },
  },
);

MyApp->setup;

Alternatively you may choose a 'shorthand' version of the configuration based approach:

package MyApp;

use Catalyst 'MapComponentDependencies';

MyApp->config(
  'Model::Foo' => { another_param => 'value' },
  'Plugin::MapComponentDependencies' => {
    'Model::Foo' => {
      bar => 'Model::Bar',
      baz => sub {
        my ($app_or_ctx, $component_name, $from_config_args) = @_;
        return ...;
      },
    },
  },
);

MyApp->setup;

You may prefer this if your dependencies will map differently based on environment and configuration settings.

Lastly you may use the helper utilities to create a single merged configuration for your dependencies:

package MyApp;

use Moose;
use Catalyst 'MapComponentDependencies;
use Catalyst::Plugin::MapComponentDependencies::Utils ':All';

MyApp->config(
  'Model::Bar' => { key => 'value' },
  'Model::Foo' => {
    bar => FromModel 'Bar',
    baz => FromCode {
      my ($app_or_ctx, $component_name) = @_;
      return ...;
    },
    another_param => 'value',
  },
);

MyApp->setup;

See Catalyst::Plugin::MapComponentDependencies::Utils for more.

DESCRIPTION

Sometimes you would like a Catalyst component to depend on the value of an existing component. Since components are resolved during application setup (or at request time, in the cause of a component that does ACCEPT_CONTEXT) you cannot specify this dependency mapping in the 'normal' Catalyst configuration hash.

This plugin, which requires a recent Catalyst of version 5.90090+, allows you to define components which depend on each other. You can also set the value of an initial argument to the value of a coderef, for added dynamic flexibility.

You may define dependencies in one of two ways. The first way is to use a key/value pair to map a configuation key attribute to the value of an existing Catalyst model. When the depending model is called, we get the value of that model in the same way as if we called '$c->model($name)'. You can by the way use any type of Catalys component as a value (models, views and even controllers).

The second way is to use a coderef, which is expected to return a value suitable for the depending model. This gives you a little more flexibility for crafting very custom types of dependencies. If you use a coderef you will get three values, the application (or context depending on if the depending model does ACCEPT_CONTEXT), the component name and a reference to any static configuration for the model (from the global configuration, for example).

NOTE: Currently we only map dependencies 'one level' into the configuration hash. Which means the following works as expected:

MyApp->map_dependencies(
  'Model::Foo' => {
    baz => sub {
      my ($app_or_ctx, $component_name, $config) = @_;
      return ...;
    },
  },
);

But not this:

MyApp->map_dependencies(
  'Model::Foo' => {
    baz => {
      bar => sub {
      my ($app_or_ctx, $component_name, $config) = @_;
      return ...;
      },
    },
  },
);

I'm not sure if I consider this limitation a feature or not... If you have good use cases let me know and I'll consider a fix.

METHODS

This plugin defines the following methods

map_dependencies

Example:

MyApp->map_dependencies(
  'Model::AnotherModel' => { aaa => 'Model::Foo' },
  'Model::Foo' => {
    bar => 'Model::Bar',
    baz => sub {
      my ($app_or_ctx, $component_name, $config) = @_;
      return ...;
    },
  },
);

Maps a list of components and dependencies.

map_dependency

Maps a single component to a hashref of dependencies.

CONFIGURATION

This plugin defines the configuration namespace 'Plugin::MapComponentDependencies' and defines the following keys:

map_dependencies

A Hashref where the key is a target component and the value is a hashref of arguments that will be sent to it during initializion.

MyApp->config(
  'Plugin::MapComponentDependencies' => {
    map_dependencies => {
      'Model::Foo' => {
        bar => 'Model::Bar',
        baz => sub {
          my ($app_or_ctx, $component_name, $from_config_args) = @_;
          return ...;
        },
      },
    },
  },
);

NOTE: for simplicity you can place your hashref of configuration directly under the plugin namespace.

MyApp->config(
  'Plugin::MapComponentDependencies' => {
    'Model::Foo' => {
      bar => 'Model::Bar',
      baz => sub {
        my ($app_or_ctx, $component_name, $from_config_args) = @_;
        return ...;
      },
    },
  },
);

SEE ALSO

Catalyst, Catalyst::Plugin::InjectionHelpers.

AUTHOR

John Napiorkowski email:[email protected]

COPYRIGHT & LICENSE

Copyright 2015, John Napiorkowski email:[email protected]

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

catalyst-plugin-mapcomponentdependencies's People

Contributors

jjn1056 avatar

Watchers

 avatar  avatar

catalyst-plugin-mapcomponentdependencies's Issues

FromController('Collection')->action_for('view'),

FromController('Collection')->action_for('view'),

this doesn't work since the object returns from FromController is just a placeholder. I bet we could do something that would make it work... or we need to add shortcut for actions..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.