Sha256: 48f5407b18a5454e0fedc3ba75dd6d5e9a0f2ffed21f7828422770f0d7d1717e

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "json"

module Statesman
  module Adapters
    class Memory
      attr_reader :transition_class
      attr_reader :parent_model

      # We only accept mode as a parameter to maintain a consistent interface
      # with other adapters which require it.
      def initialize(transition_class, parent_model, observer, _opts = {})
        @history = []
        @transition_class = transition_class
        @parent_model = parent_model
        @observer = observer
      end

      def create(from, to, metadata = {})
        from = from.to_s
        to = to.to_s
        transition = transition_class.new(to, next_sort_key, metadata)

        @observer.execute(:before, from, to, transition)
        @history << transition
        @observer.execute(:after, from, to, transition)
        @observer.execute(:after_commit, from, to, transition)
        transition
      end

      def last(*)
        @history.max_by(&:sort_key)
      end

      def history(*)
        @history
      end

      def reset
        @history = []
      end

      private

      def next_sort_key
        (last && last.sort_key + 10) || 10
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
statesman-10.0.0 lib/statesman/adapters/memory.rb
statesman-9.0.1 lib/statesman/adapters/memory.rb
statesman-9.0.0 lib/statesman/adapters/memory.rb
statesman-8.0.3 lib/statesman/adapters/memory.rb
statesman-8.0.2 lib/statesman/adapters/memory.rb
statesman-8.0.1 lib/statesman/adapters/memory.rb
statesman-8.0.0 lib/statesman/adapters/memory.rb
statesman-7.4.1 lib/statesman/adapters/memory.rb