Sha256: acb6f9501274d6882e103c0ed4a0919efac31811c3307f2594288c7a500994dc

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 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-12.1.0 lib/statesman/adapters/memory.rb
statesman-12.0.0 lib/statesman/adapters/memory.rb
statesman-11.0.0 lib/statesman/adapters/memory.rb
statesman-10.2.3 lib/statesman/adapters/memory.rb
statesman-10.2.2 lib/statesman/adapters/memory.rb
statesman-10.2.1 lib/statesman/adapters/memory.rb
statesman-10.2.0 lib/statesman/adapters/memory.rb
statesman-10.1.0 lib/statesman/adapters/memory.rb