pluginaweek / state_machine

Adds support for creating state machines for attributes on any Ruby class
http://www.pluginaweek.org
MIT License
3.74k stars 507 forks source link

Need fallback method for methods defined in state context (DRY) #278

Open DougPuchalski opened 11 years ago

DougPuchalski commented 11 years ago

It feels like a method defined in a state context should override a method defined outside of it, i.e. a normal instance method of a class.

If I define the instance method, the same-named methods in the state context are never reached. If I do not define the instance method, then there is an exception if the method is called when in any state where it is not defined. Certainly not very DRY for a default case.

DougPuchalski commented 11 years ago

Here's how I hacked my instance method definition to defer to context methods when available, required on a per-method/per-state basis unfortunately.

def message
  if (context_method = self.class.state_machines[:state].states[state].context_methods[__method__])
    context_method.bind(self).call
  else
    'default message'
  end
end