state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Should state machine methods override existing class methods? #85

Closed vanboom closed 2 years ago

vanboom commented 2 years ago

I think this concept could be argued either way so I am presenting it as a question than an issue. In this trivial example, the class has a method importance. When using the state machine, I expected the importance method to be overridden by the state machine when state == :on, but that does not happen.

Should the state machine override the method when the state applies?

class MyModel
  def importance
     99
  end

  state_machine do
    state :on do
       def importance
         1
       end
    end
    state :off do
    end
  end
end
> m.state = :on
> m.importance
99     <---- expected to be 1, the state machine method is not being used

Thank you!

seuros commented 2 years ago

In the README, check the Vehicle class. The speed changes correctly.

You have to define the importance inside the off state or a default state.