Class: Midnight::Rails::ActiveRecordEventAdapter
- Inherits:
-
Object
- Object
- Midnight::Rails::ActiveRecordEventAdapter
- Defined in:
- lib/midnight/rails/active_record_event_adapter.rb
Overview
noinspection RubyDefParenthesesInspection noinspection RubyParameterNamingConvention
Instance Attribute Summary collapse
-
#active_record ⇒ Object
readonly
Returns the value of attribute active_record.
Instance Method Summary collapse
-
#initialize(active_record: DefaultEvent, serializer: Utilities::JsonSerializer, fetch_event_timestamp: lambda do |event| ::Time.parse(event.metadata[:occurred_at]) end) ⇒ ActiveRecordEventAdapter
constructor
A new instance of ActiveRecordEventAdapter.
- #save_events(events = [], next_position:, state_id:, **_) ⇒ Object
Constructor Details
#initialize(active_record: DefaultEvent, serializer: Utilities::JsonSerializer, fetch_event_timestamp: lambda do |event| ::Time.parse(event.metadata[:occurred_at]) end) ⇒ ActiveRecordEventAdapter
Returns a new instance of ActiveRecordEventAdapter.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/midnight/rails/active_record_event_adapter.rb', line 12 def initialize(\ active_record: DefaultEvent, serializer: Utilities::JsonSerializer, fetch_event_timestamp: lambda do |event| ::Time.parse(event.[:occurred_at]) end ) @active_record = active_record @serializer = serializer @fetch_event_timestamp = freeze end |
Instance Attribute Details
#active_record ⇒ Object (readonly)
Returns the value of attribute active_record.
10 11 12 |
# File 'lib/midnight/rails/active_record_event_adapter.rb', line 10 def active_record @active_record end |
Instance Method Details
#save_events(events = [], next_position:, state_id:, **_) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/midnight/rails/active_record_event_adapter.rb', line 25 def save_events(events = [], next_position:, state_id:, **_) serialized_events = events.each_with_index.map do |unserialized_event, position_offset| { event_type: unserialized_event.class.name, data: @serializer.dump(unserialized_event.data), metadata: @serializer.dump(unserialized_event.), position: next_position + position_offset, state_id: state_id, occurred_at: @fetch_event_timestamp.call(unserialized_event), } end # https://github.com/zdennis/activerecord-import active_record.import(serialized_events, validate: false) end |