Improve the horrific api of emitters #13

Closed
cakey opened this Issue Apr 25, 2012 · 3 comments

Comments

Projects
None yet
1 participant
@cakey
Owner

cakey commented Apr 25, 2012

No description provided.

@cakey

This comment has been minimized.

Show comment Hide comment
@cakey

cakey Apr 25, 2012

Owner

Before:

from Shimmer.rest_framework import Emitter
import myapp.models as mod

class AnEmitter(Emitter):
    def construct(self, data):
        """
            Calls the base constructor with the manips/massagers you want.
        """     
        manips = [self.get_locations, self.get_comments]

        massagers = { mod.Event:self.massage_event}

        return self._construct(data, manips=manips, massagers=massagers)

    def get_locations(self, data, ids):
        """
            This is an example data collection function, that collates a certain type of information we need.
            Data and ids are dictionaries that store the data we need
        """
        data['locations'] = mod.Location.objects.in_bulk(list(ids['locations']))
        return data, ids

    def get_comments(self, data, ids):
        data['comments'] = mod.Location.objects.in_bulk(list(ids['comments']))
        return data, ids

    def massage_event(self, model_dict, data
        """
            Example of an massager to change the output of a model instance.
        """
        if self._pre:
            self._ids['locations'].add(model_dict['location_id']
            self._ids['comments'].add(model_dict['comment_id']
        else:
            model_dict['location'] = self._any(self.data['locations'][model_dict['location_id'])
            model_dict['comment'] = self._any(self.data['comments'][model_dict['comment_id'])

            del model_dict['location_id']
            del model_dict['comment_id']

        return model_dict
Owner

cakey commented Apr 25, 2012

Before:

from Shimmer.rest_framework import Emitter
import myapp.models as mod

class AnEmitter(Emitter):
    def construct(self, data):
        """
            Calls the base constructor with the manips/massagers you want.
        """     
        manips = [self.get_locations, self.get_comments]

        massagers = { mod.Event:self.massage_event}

        return self._construct(data, manips=manips, massagers=massagers)

    def get_locations(self, data, ids):
        """
            This is an example data collection function, that collates a certain type of information we need.
            Data and ids are dictionaries that store the data we need
        """
        data['locations'] = mod.Location.objects.in_bulk(list(ids['locations']))
        return data, ids

    def get_comments(self, data, ids):
        data['comments'] = mod.Location.objects.in_bulk(list(ids['comments']))
        return data, ids

    def massage_event(self, model_dict, data
        """
            Example of an massager to change the output of a model instance.
        """
        if self._pre:
            self._ids['locations'].add(model_dict['location_id']
            self._ids['comments'].add(model_dict['comment_id']
        else:
            model_dict['location'] = self._any(self.data['locations'][model_dict['location_id'])
            model_dict['comment'] = self._any(self.data['comments'][model_dict['comment_id'])

            del model_dict['location_id']
            del model_dict['comment_id']

        return model_dict
@cakey

This comment has been minimized.

Show comment Hide comment
@cakey

cakey Apr 25, 2012

Owner

After

from Shimmer.rest_framework import Emitter
import myapp.models as mod

class AnEmitter(Emitter):
    def __init__(self):
        """
            Calls the base constructor with the manips/massagers you want.
        """     
        self.manips = [self.get_locations, self.get_comments]

        self.massagers = { mod.Event:self.massage_event}

    def get_locations(self):
        """
            This is an example data collection function, that collates a certain type of information we need.
            Data and ids are dictionaries that store the data we need
        """
        self.data['locations'] = mod.Location.objects.in_bulk(list(self.ids['locations']))

    def get_comments(self):
        self.data['comments'] = mod.Comment.objects.in_bulk(list(self.ids['comments']))

    def massage_event(self, model_dict, model_instance):
        """
            Example of an massager to change the output of a model instance.
        """
        if self.collecting:
            self.ids['locations'].add(model_dict['location_id']
            self.ids['comments'].add(model_dict['comment_id']
        else:
            model_dict['location'] = self.construct(self.data['locations'][model_dict['location_id'])
            model_dict['comment'] = self.construct(self.data['comments'][model_dict['comment_id'])

            del model_dict['location_id']
            del model_dict['comment_id']

        return model_dict
Owner

cakey commented Apr 25, 2012

After

from Shimmer.rest_framework import Emitter
import myapp.models as mod

class AnEmitter(Emitter):
    def __init__(self):
        """
            Calls the base constructor with the manips/massagers you want.
        """     
        self.manips = [self.get_locations, self.get_comments]

        self.massagers = { mod.Event:self.massage_event}

    def get_locations(self):
        """
            This is an example data collection function, that collates a certain type of information we need.
            Data and ids are dictionaries that store the data we need
        """
        self.data['locations'] = mod.Location.objects.in_bulk(list(self.ids['locations']))

    def get_comments(self):
        self.data['comments'] = mod.Comment.objects.in_bulk(list(self.ids['comments']))

    def massage_event(self, model_dict, model_instance):
        """
            Example of an massager to change the output of a model instance.
        """
        if self.collecting:
            self.ids['locations'].add(model_dict['location_id']
            self.ids['comments'].add(model_dict['comment_id']
        else:
            model_dict['location'] = self.construct(self.data['locations'][model_dict['location_id'])
            model_dict['comment'] = self.construct(self.data['comments'][model_dict['comment_id'])

            del model_dict['location_id']
            del model_dict['comment_id']

        return model_dict
@cakey

This comment has been minimized.

Show comment Hide comment
@cakey

cakey Apr 25, 2012

Owner

self.contruct

Owner

cakey commented Apr 25, 2012

self.contruct

@cakey cakey closed this in 80d8302 Apr 25, 2012

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