module Workflow::ClassMethods

Attributes

workflow_spec[R]

Public Instance Methods

workflow(&specification) click to toggle source
# File lib/workflow.rb, line 23
def workflow(&specification)
  assign_workflow Specification.new(Hash.new, &specification)
end
workflow_column(column_name=nil) click to toggle source

Workflow does not provide any state persistence - it is the job of particular persistence libraries for workflow and activerecord or remodel. But it still makes sense to provide a default name and override feature.

# File lib/workflow.rb, line 13
def workflow_column(column_name=nil)
  if column_name
    @workflow_state_column_name = column_name.to_sym
  end
  if !instance_variable_defined?('@workflow_state_column_name') && superclass.respond_to?(:workflow_column)
    @workflow_state_column_name = superclass.workflow_column
  end
  @workflow_state_column_name ||= :workflow_state
end

Private Instance Methods

assign_workflow(specification_object) click to toggle source

Creates the convinience methods like `my_transition!`

# File lib/workflow.rb, line 30
def assign_workflow(specification_object)

  # Merging two workflow specifications can **not** be done automically, so
  # just make the latest specification win. Same for inheritance -
  # definition in the subclass wins.
  if respond_to? :inherited_workflow_spec # undefine methods defined by the old workflow_spec
    inherited_workflow_spec.states.values.each do |state|
      state_name = state.name
      module_eval do
        undef_method "#{state_name}?"
      end

      state.events.flat.each do |event|
        event_name = event.name
        module_eval do
          undef_method "#{event_name}!".to_sym
          undef_method "can_#{event_name}?"
        end
      end
    end
  end

  @workflow_spec = specification_object
  @workflow_spec.states.values.each do |state|
    state_name = state.name
    module_eval do
      define_method "#{state_name}?" do
        state_name == current_state.name
      end
    end

    state.events.flat.each do |event|
      event_name = event.name
      module_eval do
        define_method "#{event_name}!".to_sym do |*args|
          process_event!(event_name, *args)
        end

        define_method "can_#{event_name}?" do
          return !!current_state.events.first_applicable(event_name, self)
        end
      end
    end
  end
end