Skip to content

Fixed adding hooks to fields defined in parent classes#11

Open
kevinastone wants to merge 1 commit intobninja:masterfrom
kevinastone:hooks-in-subclasses
Open

Fixed adding hooks to fields defined in parent classes#11
kevinastone wants to merge 1 commit intobninja:masterfrom
kevinastone:hooks-in-subclasses

Conversation

@kevinastone
Copy link

Ran into a situation where I wanted to define a generic field in the parent class, but wanted to be able to override the value using a @compute hook in child classes.

It's not ideal having to clone any field you override, but it at least works correctly once you have the fields setup.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 60.4% when pulling 4d0c505 on kevinastone:hooks-in-subclasses into a62f43c on bninja:master.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not that its any better (still tedious) but u could do ~

class Item(pilo.Form):

    price = pilo.fields.Integer()

    @price.compute
    def price(self):
        return self.compute_price()

    def compute_price(self):
        return type(self).price._compute()


class Heirloom(Item):

    has_sentimental_value = pilo.fields.Boolean()

    def compute_price(self):
        return 1000000 if self.has_sentimental_value else 0

@bninja
Copy link
Owner

bninja commented Jun 3, 2015

seems ok (hack workaround here if need it). but maybe a better interface would be ~:

class Item(pilo.Form):

    price = pilo.fields.Integer()


class Heirloom(Item):

    has_sentimental_value = pilo.fields.Boolean()

    @pilo.hook.compute
    def price(self):
        return 1000000 if self.has_sentimental_value else 0

don't know if it can be done (have base fields and names, so seems so). this looks like ~ extension of polymorphic stuff to field hooks.

@kevinastone
Copy link
Author

From an API perspective, I think it would be better if the hook was offered late binding so I could simple register them such against the base class field.

class Item(pilo.Form):
    price = pilo.fields.Integer()

class Heirloom(Item):

    has_sentimental_value = pilo.fields.Boolean()

    @Item.price.compute
    def price(self):
        return 1000000 if self.has_sentimental_value else 0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants