Coder Social home page Coder Social logo

plone.alterego's Introduction

==============
plone.alterego
==============

Now you see it, it now you don't!

This package defines a dynamic module type that lets you create objects in the
dynamic module on demand.

Usage
-----

To use this package, you should:

 - Identify an appropriate parent module where the dynamic module will live.

 - Ensure that plone.alterego.dynamic.create() is called with this module and
   a dynamic module name. Typically, you'd do this in the parent module
   itself, so that the dynamic module is instantiated as soon as the parent
   module is imported.

 - Register a named utility providing IDynamicObjectFactory. The name should
   be the same as the full dotted path to the dynamic module. This utility
   will be responsible for creating the objects that inhabit the dynamic
   module.

Example
-------

For a more fully-featured example, see the alterego.txt doctest.

Let's say we have a generic content class that should get a unique interface
for each instance.

    >>> from zope import interface
    >>> class IContent(interface.Interface):
    ...     pass
    >>> class Content(object):
    ...     interface.implements(IContent)

    >>> c1 = Content()

To create the unique interface, we will use a dynamic module. There is a
helper method to make this easier. It takes a parent module and a name as
arguments:

    >>> from plone.alterego.dynamic import create
    >>> dynamic = create('plone.alterego.tests.dynamic')

We can now import this module:

    >>> from plone.alterego.tests import dynamic

To make objects on demand, we'll need to register a utility that can act
as a factory.

    >>> from plone.alterego.interfaces import IDynamicObjectFactory
    >>> from zope.interface.interface import InterfaceClass
    >>> class InterfaceOnDemand(object):
    ...     interface.implements(IDynamicObjectFactory)
    ...
    ...     def __call__(self, name, module):
    ...         schema = InterfaceClass(name, (interface.Interface,), __module__=module.__name__)
    ...         setattr(module, name, schema)
    ...         return schema

This utility should have a name that corresponds to the full,
dotted name to the dynamic module. This way, we can have different factories
for different dynamic modules. We'd register this in ZCML like so::

    <utility
        name="plone.alterego.tests.dynamic"
        provides="plone.alterego.interfaces.IDynamicObjectFactory"
        factory=".factory.InterfaceOnDemand"
        />

From this point forward, when we access an attribute of the dynamic module,
the factory will be used:

    >>> dynamic.IOne
    <InterfaceClass plone.alterego.tests.dynamic.IOne>

Note that so long as the setattr() call above is executed, the factory is
called only once. That is, you'll always get the same object each time you
access a given attribute of the dynamic module.

plone.alterego's People

Contributors

optilude avatar mauritsvanrees avatar gforcada avatar davisagli avatar datakurre avatar bloodbare avatar

Watchers

Timo Stollenwerk avatar James Cloos avatar

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.