adomokos / light-service

Series of Actions with an emphasis on simplicity.
MIT License
837 stars 67 forks source link

Assign expected and promised keys to Action subclass #72

Closed jpmoral closed 9 years ago

jpmoral commented 9 years ago

Currently when an Action is subclassed the subclass cannot access the expected and promised keys using dot notation.

class AnAction
  extend LightService::Action
  expects :expected_key
  executed do |context|
    # do stuff
  end
end

class AnActionSubclass
  executed do |context|
    # do stuff
    context.expected_key # raises an error
  end
end

This PR will allow the above to work.

jpmoral commented 9 years ago

@adomokos

I discovered this a couple of weeks ago. I was able to eliminate the need for subclassing by going with a different (cleaner) design of Action and Context hash so I'm not sure how useful this is.

However, I made the PR because it might be surprising to others that expects and promises don't work with inheritance.

adomokos commented 9 years ago

@jpmoral, thank you for submitting this PR.

The reason I opted to use class methods for the actions was to limit inheritance. With inheritance there is an "inferred" state that I tried to avoid.

Would you be OK if I did not merge this PR? I know you worked on it, that's why I feel bad about it.

jpmoral commented 9 years ago

@adomokos

It's no problem, like I mentioned, even I'm a bit iffy about it. It might be helpful though to be explicit in the docs that inheritance of actions isn't really encouraged.

adomokos commented 9 years ago

Good suggestion!