Module: Sandthorn::EventInspector

Defined in:
lib/sandthorn/event_inspector.rb

Instance Method Summary collapse

Instance Method Details

#events_with_trace_infoObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sandthorn/event_inspector.rb', line 31

def events_with_trace_info
  saved = Sandthorn.get_aggregate_events self.aggregate_id, self.class 
  unsaved = self.aggregate_events
  all = saved.concat(unsaved).sort { |a, b| a[:aggregate_version] <=> b[:aggregate_version] }
  extracted = all.collect do |e| 
    if e[:event_args].nil? && !e[:event_data].nil?
      data = Sandthorn.deserialize e[:event_data]
    else
      data = e[:event_args]
    end
    trace = data[:trace] unless data.nil? || !data.is_a?(Hash)
    {aggregate_version: e[:aggregate_version], event_name: e[:event_name].to_sym, trace: trace }  
  end
  return extracted
end

#has_event?(event_name, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/sandthorn/event_inspector.rb', line 25

def has_event? event_name, options = {}
  matching_events = events_with_trace_info.select { |e| e[:event_name] == event_name }
  event_exists = matching_events.length > 0
  trace = has_trace? matching_events, options.fetch(:trace, {})
  return event_exists && trace
end

#has_saved_event?(event_name, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/sandthorn/event_inspector.rb', line 16

def has_saved_event? event_name, options = {}
  saved = events_with_trace_info
  saved.reject! { |e| e[:aggregate_version] >= self.aggregate_events.first[:aggregate_version] } unless self.aggregate_events.empty?
  matching_events = saved.select { |e| e[:event_name] == event_name }
  event_exists = matching_events.length > 0
  trace = has_trace? matching_events, options.fetch(:trace, {})

  return event_exists && trace
end

#has_unsaved_event?(event_name, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/sandthorn/event_inspector.rb', line 3

def has_unsaved_event? event_name, options = {}
  unsaved = events_with_trace_info
  if self.aggregate_events.empty?
    unsaved = []
  else
    unsaved.reject! { |e| e[:aggregate_version] < self.aggregate_events.first[:aggregate_version] } 
  end
  matching_events = unsaved.select { |e| e[:event_name] == event_name }
  event_exists = matching_events.length > 0
  trace = has_trace? matching_events, options.fetch(:trace, {})

  return event_exists && trace
end