module MinistryOfState::ClassMethods

Public Instance Methods

add_event(name, options = {}) { || ... } click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 73
    def add_event(name, options = {},&block)
      opts = yield
      event_options = opts.merge(options)
      event = MosEvent.new(name.to_s, @mos_current_column_, event_options)
      self.events.merge!(name.to_s => event)
      class_eval <<-RUBY,__FILE__,__LINE__+1
        def #{name.to_s}!
          fire('#{name}')
        end
      RUBY
    end
add_initial_state(name) click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 61
def add_initial_state(name)
  add_state(name, :initial => true)
end
add_state(name, options={}) click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 65
    def add_state(name, options={})
      state = MosState.new(name.to_s, @mos_current_column_, options)
      self.states.merge!(name.to_s => state)
      class_eval <<-RUBY,__FILE__,__LINE__+1
        def #{name.to_s}?; check_state('#{name}'); end
      RUBY
    end
get_initial_state_for(column) click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 45
def get_initial_state_for(column)
  self.states.each do |name, state|
    return state if column == state.column && state.initial?
  end
  nil
end
ministry_of_state(column) { || ... } click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 52
def ministry_of_state(column)
  prepare_mos_attributes
  @mos_current_column_ = column
  yield

  initial_state = get_initial_state_for(column)
  raise NoInitialState.new("You need to specify initial state") unless initial_state
end
prepare_mos_attributes() click to toggle source

create a hash for states

# File lib/ministry_of_state/ministry_of_state.rb, line 32
def prepare_mos_attributes
  unless self.respond_to?(:states)
    class_attribute :states
    class_attribute :events

    self.states = HashWithIndifferentAccess.new
    self.events = HashWithIndifferentAccess.new

    after_initialize :set_initial_states_for_new_record
    after_create  :run_initial_state_actions
  end
end
transitions(opts = {}) click to toggle source
# File lib/ministry_of_state/ministry_of_state.rb, line 85
def transitions(opts = {})
  if opts[:from] == :any
    opts[:from] = self.states.keys
  end

  if opts[:from].blank? || opts[:to].blank?
    raise TransitionNotAllowed.new("You need to specify from and to states")
  end
  opts[:from] = Array(opts[:from]).map(&:to_sym)
  {:from => opts[:from], :to => opts[:to], :guard => opts[:guard] }
end