title: Rails Workflow Gem - Metaprogramming events into named_scopes?
body: <p>I'm using <a href="http://github.com/geekq/workflow" rel="noreferrer">http://github.com/geekq/workflow</a> to provide a state machine. I'm using ActiveRecord to save state, which means I have a "workflow_state" attribute in the model. I think I want a named_scope for each event in the state machine, so I can find all objects in a given state. For example, assuming a very simple state machine:</p>
  
  <pre><code>workflow do
    state :new do
      event :time_passes, :transitions_to =&gt; :old
    end
    state :old do
      event :death_arrives, :transitions_to =&gt; :dead
    end
    state :dead
  end
  </code></pre>
  
  <p>I want named scopes for each state. However, that's not DRY... What I want to end up with is something like:</p>
  
  <pre><code>named_scope :new, :conditions =&gt; ['workflow_state = ?', 'new']
  named_scope :old, :conditions =&gt; ['workflow_state = ?', 'old']
  named_scope :dead, :conditions =&gt; ['workflow_state = ?', 'dead']
  </code></pre>
  
  <p>But with a few lines that don't depend on the current list of states. </p>
  
  <p>I can see that Model#workflow_spec.states.keys gives me each state. But what I think I need is a wierd lambda where the name of the scope is a variable. And I have no idea how to do that. At all. Been staring at this for hours and playing with irb, but I think there's a piece of knowledge about metaprogramming that I just don't have. Help, please!</p>
  
  <p>Lucas, below, gives the answer - but we also need to change a symbol to a string:</p>
  
  <pre><code>  workflow_spec.states.keys.each do |state|
       named_scope state, :conditions =&gt; ['workflow_state = ?', state.to_s] 
    end
  </code></pre>