Module: RubyEventStore::ProcessManager

Defined in:
lib/ruby_event_store/process_manager.rb,
lib/ruby_event_store/process_manager/retry.rb,
lib/ruby_event_store/process_manager/version.rb,
lib/ruby_event_store/process_manager/state_replay.rb,
lib/ruby_event_store/process_manager/browser_extension.rb

Defined Under Namespace

Modules: ProcessMethods, Retry, Subscriptions Classes: BrowserExtension, StateReplay

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.parse_stream_name(stream_name) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/ruby_event_store/process_manager.rb', line 72

def self.parse_stream_name(stream_name)
  class_name, _, id = stream_name.to_s.partition("$")
  return if id.empty?

  process_class = @registered_process_managers[class_name]
  return unless process_class

  [process_class, id]
end

.register(process_class) ⇒ Object



68
69
70
# File 'lib/ruby_event_store/process_manager.rb', line 68

def self.register(process_class)
  @registered_process_managers[process_class.name] = process_class if process_class.name
end

.with_state(&state_class_block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_event_store/process_manager.rb', line 82

def self.with_state(&state_class_block)
  unless block_given?
    raise ArgumentError, "A block returning the state class is required."
  end

  Module.new do
    @state_definition_block = state_class_block

    define_method(:initial_state) do
      block = self.class.instance_variable_get(:@state_definition_block)
      raise "State definition block not found on #{self.class}" unless block

      state_class = block.call
      raise "State definition block did not return a Class" unless state_class.instance_of?(Class)

      state_class.new
    end

    define_method(:state) do
      @state
    end

    def self.included(host_class)
      host_class.instance_variable_set(:@state_definition_block, @state_definition_block)

      host_class.include(ProcessMethods)
      host_class.include(Retry)
      host_class.extend(Subscriptions)
      ProcessManager.register(host_class)
    end
  end
end