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

state machines definition inheritance #32

Closed
dmgr opened this issue Mar 8, 2015 · 3 comments
Closed

state machines definition inheritance #32

dmgr opened this issue Mar 8, 2015 · 3 comments

Comments

@dmgr
Copy link

dmgr commented Mar 8, 2015

Why inherited state machines doesn't inherit callbacks and what can be done to fix it?

For example SpecificStateMachine that inherits from GenericStateMachine doesn't inherit "on_enter" callback:

  class GenericStateMachine < FiniteMachine::Definition
    callbacks do
      on_enter do |event|
        target.state = event.to
      end
    end
  end

  class SpecificStateMachine < GenericStateMachine
    callbacks do
      on_after :accept do |event|
        #TODO
      end
    end
  end
@dmgr
Copy link
Author

dmgr commented Mar 8, 2015

I found a workaround, but first snippet was intuitive and logical IMO:

  class GenericStateMachine < FiniteMachine::Definition
    def self.inherited subclass
      subclass.callbacks do
        on_enter do |event|
          target.state = event.to
        end
      end

      super
    end
  end

@piotrmurach
Copy link
Owner

The FiniteMachine::Definition is just sugar for the define method. It decoares the class with class instance methods and thus doesn't really provide inheritance as such but a more conveninent way to factor state machine instances. I'm thinking it should probably be rewritten to module to better express what it does. I find your example interesting. Could you provide more background why such inheritane would be useful? More code would be even better, would you mind provided a fuller example? I haven't had any need thus far to use the inheritance but I'm more than happy to work with you on this!

@piotrmurach
Copy link
Owner

@dmgr I've implemented definition inheritance. Your initial example should work like a charm now.

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

No branches or pull requests

2 participants