Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Meta handling #87

Closed
derks opened this issue Dec 23, 2011 · 2 comments
Closed

Refactor Meta handling #87

derks opened this issue Dec 23, 2011 · 2 comments

Comments

@derks
Copy link
Member

derks commented Dec 23, 2011

Right now handlers require a Meta class to provide metadata attributes ... should refactor how that works to say.. have default 'self._meta' which is a Meta() class... any data set via self.Meta would be merge (override self._meta.attr if self.Meta.attr exists) ... and then on the backend use self._meta as the final meta data.

For example, this is pulled from Slumber:

class Meta(object):
    """
    Model that acts as a container class for a meta attributes for a larger
    class. It stuffs any kwarg it gets in it's init as an attribute of itself.
    """

    def __init__(self, **kwargs):
        for key, value in kwargs.iteritems():
            setattr(self, key, value)


class MetaMixin(object):
    """
    Mixin that provides the Meta class support to add settings to instances
    of slumber objects. Meta settings cannot start with a _.
    """

    def __init__(self, *args, **kwargs):
        # Get a List of all the Classes we in our MRO, find any attribute named
        #     Meta on them, and then merge them together in order of MRO
        metas = reversed([x.Meta for x in self.__class__.mro() if hasattr(x, "Meta")])
        final_meta = {}

        # Merge the Meta classes into one dict
        for meta in metas:
            final_meta.update(dict([x for x in meta.__dict__.items() if not x[0].startswith("_")]))

        # Update the final Meta with any kwargs passed in
        for key in final_meta.keys():
            if key in kwargs:
                final_meta[key] = kwargs.pop(key)

        self._meta = Meta(**final_meta)

        # Finally Pass anything unused along the MRO
        super(MetaMixin, self).__init__(*args, **kwargs)

Something like that... not exactly, but something more structured.

@ghost ghost assigned derks Dec 23, 2011
@derks
Copy link
Member Author

derks commented Jan 11, 2012

Ensure any meta from inheritted classes is 'mixed in' first, then overridden by the classes meta.... yeah, the above does that via MRO.

derks pushed a commit that referenced this issue Jan 26, 2012
@derks
Copy link
Member Author

derks commented Feb 2, 2012

95e89fa Resolved this.... but needs testing.

@derks derks closed this as completed Feb 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant