Sha256: 5b2e471efd7c6c8d7da07cefe31a9a0fd7f50ddd8717a8faddfcd6c6afdd1346

Contents?: true

Size: 938 Bytes

Versions: 25

Compression:

Stored size: 938 Bytes

Contents

# frozen_string_literal: true
module Dynflow
  module Stateful
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      def states
        raise NotImplementedError
      end

      def state_transitions
        raise NotImplementedError
      end
    end

    def states
      self.class.states
    end

    def state_transitions
      self.class.state_transitions
    end

    attr_reader :state

    def state=(state)
      set_state state, false
    end

    def set_state(state, skip_transition_check)
      state = state.to_sym if state.is_a?(String) && states.map(&:to_s).include?(state)
      raise "unknown state #{state}" unless states.include? state
      unless self.state.nil? || skip_transition_check || state_transitions.fetch(self.state).include?(state)
        raise "invalid state transition #{self.state} >> #{state} in #{self}"
      end
      @state = state
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
dynflow-1.8.2 lib/dynflow/stateful.rb
dynflow-1.8.1 lib/dynflow/stateful.rb
dynflow-1.8.0 lib/dynflow/stateful.rb
dynflow-1.7.0 lib/dynflow/stateful.rb
dynflow-1.6.11 lib/dynflow/stateful.rb
dynflow-1.6.10 lib/dynflow/stateful.rb
dynflow-1.6.8 lib/dynflow/stateful.rb
dynflow-1.6.7 lib/dynflow/stateful.rb
dynflow-1.6.6 lib/dynflow/stateful.rb
dynflow-1.6.5 lib/dynflow/stateful.rb
dynflow-1.6.4 lib/dynflow/stateful.rb
dynflow-1.6.3 lib/dynflow/stateful.rb
dynflow-1.6.2 lib/dynflow/stateful.rb
dynflow-1.6.1 lib/dynflow/stateful.rb
dynflow-1.4.9 lib/dynflow/stateful.rb
dynflow-1.4.8 lib/dynflow/stateful.rb
dynflow-1.5.0 lib/dynflow/stateful.rb
dynflow-1.4.7 lib/dynflow/stateful.rb
dynflow-1.4.6 lib/dynflow/stateful.rb
dynflow-1.4.5 lib/dynflow/stateful.rb