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

Allow events to define a transition_missing hook. #40

Merged
merged 2 commits into from Aug 8, 2013
Merged

Allow events to define a transition_missing hook. #40

merged 2 commits into from Aug 8, 2013

Conversation

jimsynz
Copy link
Contributor

@jimsynz jimsynz commented Aug 7, 2013

I've added a transition_missing hook so that events can implement
their own logic for dealing with events received in an improper state.

For example:

class Order
  stateflow do
    initial :received
    state   :received,:paid,:shipped,:cancelled

    event :payment_received do
      transitions from: :received, to: :paid
    end

    event :shipment_sent do
      transitions from: :paid, to: :shipped
    end

    event :cancel do
      transitions from: :received, to: :cancelled
      transitions from: :paid,     to: :cancelled

      transition_missing do |from_state|
        if from_state == 'shipped'
          raise "Can't cancel a shipped order."
        end
      end
    end
  end
end

This allows a user to define their own Exception to be raised, or,
in fact any logic (like send another event to move into an error state).

Expecially because the rvmrc was pointing at an ancient version of Ruby
that I don't even have installed.
I've added a `transition_missing` hook so that events can implement
their own logic for dealing with events received in an improper state.

For example:

```ruby
class Order
  stateflow do
    initial :received
    state   :received,:paid,:shipped,:cancelled

    event :payment_received do
      transitions from: :received, to: :paid
    end

    event :shipment_sent do
      transitions from: :paid, to: :shipped
    end

    event :cancel do
      transitions from: :received, to: :cancelled
      transitions from: :paid,     to: :cancelled

      transition_missing do |from_state|
        if from_state == 'shipped'
          raise "Can't cancel a shipped order."
        end
      end
    end
  end
end
```

This allows a user to define their own Exception to be raised, or,
in fact any logic (like send another event to move into an error state).
@ryanza
Copy link
Owner

ryanza commented Aug 8, 2013

Cool looks good to me! :) Thanks for this. Ill be getting around to cleaning things up for a new release soon!

ryanza added a commit that referenced this pull request Aug 8, 2013
Allow events to define a transition_missing hook.
@ryanza ryanza merged commit 1be7949 into ryanza:master Aug 8, 2013
@jimsynz
Copy link
Contributor Author

jimsynz commented Aug 8, 2013

No problem. Just needed it for an app I'm working on and it seemed like a fairly easy thing to add.

@jimsynz jimsynz deleted the custom-exceptions branch August 8, 2013 09:10
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.

None yet

2 participants