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

Feature proposal: Allow accessing states as state machine constant #513

Open
stephannv opened this issue Jun 29, 2023 · 2 comments · May be fixed by #515
Open

Feature proposal: Allow accessing states as state machine constant #513

stephannv opened this issue Jun 29, 2023 · 2 comments · May be fixed by #515

Comments

@stephannv
Copy link

Would be nice if .state method defined a constant using the state name. eg.:

class OrderStateMachine
  include Statesman::Machine
  
  state :pending
  state :failed
end

OrderStateMachine::PENDING # => "pending"
OrderStateMachine::FAILED # => "failed"

This way we can detect typos and changed states early:

Order.new(state: :peding) # ok
Order.in_state(:peding) # ok

Order.new(state: OrderStateMachine::PEDING) # uninitialized constant OrderStateMachine::PEDING (NameError)
Order.in_state(OrderStateMachine::PEDING) # uninitialized constant OrderStateMachine::PEDING (NameError)

What do you think? Would this be useful for more people?

@stephannv stephannv linked a pull request Aug 2, 2023 that will close this issue
@dmitry
Copy link
Contributor

dmitry commented Sep 2, 2023

Useful from the perspective of refactoring and finding the usage, as in our codebase some of the states from different state machines are equally named. The main question should it be a symbol or a string. And what about the case, when the constant name is already defined? Should it redefine it quietly, raise an error or show a message while the application boots?!

@stephannv
Copy link
Author

stephannv commented Sep 2, 2023

@dmitry following .initial_state, #current_state, .states return type, it should be a string. About constant name conflict, I think it should just warn user about it.

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 a pull request may close this issue.

2 participants