From dce1e25d8c92c6d19dbecca44a678754ee66047b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pacana?= Date: Tue, 5 Jan 2021 22:09:59 +0100 Subject: [PATCH] Allow to change event type resolver in include AR module * harmonizes with https://github.com/RailsEventStore/rails_event_store/pull/837 --- aggregate_root/lib/aggregate_root.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aggregate_root/lib/aggregate_root.rb b/aggregate_root/lib/aggregate_root.rb index a0ba5fd94d..269cdfc579 100644 --- a/aggregate_root/lib/aggregate_root.rb +++ b/aggregate_root/lib/aggregate_root.rb @@ -14,7 +14,7 @@ module OnDSL def on(*event_klasses, &block) event_klasses.each do |event_klass| - name = event_klass.to_s + name = event_type_for(event_klass) raise(ArgumentError, "Anonymous class is missing name") if name.start_with? ANONYMOUS_CLASS handler_name = "on_#{name}" @@ -80,11 +80,14 @@ def self.with_strategy(strategy) with(strategy: strategy) end - def self.with(strategy: ->{ DefaultApplyStrategy.new }) + def self.with(strategy: ->{ DefaultApplyStrategy.new }, event_type_resolver: ->(value) { value.to_s }) Module.new do - def self.included(host_class) + define_singleton_method :included do |host_class| host_class.extend Constructor host_class.include AggregateMethods + host_class.define_singleton_method :event_type_for do |value| + event_type_resolver.call(value) + end end define_method :apply_strategy do