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

Comments

Projects
None yet
1 participant
@derks
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

This comment has been minimized.

Show comment
Hide comment
@derks

derks Jan 11, 2012

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@derks

derks Feb 2, 2012

Member

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

Member

derks commented Feb 2, 2012

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

@derks derks closed this Feb 18, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment