Module: Eventsimple::Event::InstanceMethods

Defined in:
lib/eventsimple/event.rb

Overview

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

Instance Method Summary collapse

Instance Method Details

#aggregateObject



103
104
105
# File 'lib/eventsimple/event.rb', line 103

def aggregate
  public_send(_aggregate_klass.model_name.element)
end

#aggregate=(aggregate) ⇒ Object



107
108
109
# File 'lib/eventsimple/event.rb', line 107

def aggregate=(aggregate)
  public_send(:"#{_aggregate_klass.model_name.element}=", aggregate)
end

#apply(aggregate) ⇒ Object

Apply the event to the aggregate passed in. The default behaviour is a no-op



59
# File 'lib/eventsimple/event.rb', line 59

def apply(aggregate); end

#apply_and_persistObject

Apply the transformation to the aggregate and save it.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/eventsimple/event.rb', line 87

def apply_and_persist
  apply_timestamps(aggregate)
  apply(aggregate)

  # Persist!
  aggregate.enable_writes!
  aggregate.save!
  aggregate.readonly!

  self.aggregate = aggregate
end

#apply_timestamps(aggregate) ⇒ Object



65
66
67
68
# File 'lib/eventsimple/event.rb', line 65

def apply_timestamps(aggregate)
  aggregate.created_at ||= created_at
  aggregate.updated_at = created_at
end

#can_apply?(_aggregate) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/eventsimple/event.rb', line 61

def can_apply?(_aggregate)
  true
end

#dispatchObject



99
100
101
# File 'lib/eventsimple/event.rb', line 99

def dispatch
  EventDispatcher.dispatch(self) unless skip_dispatcher
end

#extend_validationObject



81
82
83
84
# File 'lib/eventsimple/event.rb', line 81

def extend_validation
  validate_form = self.class.instance_variable_get(:@validate_with)
  self.aggregate = aggregate.extend(validate_form) if validate_form
end

#perform_transition_checksObject

Raises:

  • (ActiveRecord::Rollback)


70
71
72
73
74
75
76
77
78
79
# File 'lib/eventsimple/event.rb', line 70

def perform_transition_checks
  return if skip_apply_check
  return if can_apply?(aggregate)

  _on_invalid_transition.call(
    Eventsimple::InvalidTransition.new(self.class),
  )

  raise ActiveRecord::Rollback
end

#skip_apply_checkObject



54
55
56
# File 'lib/eventsimple/event.rb', line 54

def skip_apply_check
  @skip_apply_check || false
end

#skip_dispatcherObject



50
51
52
# File 'lib/eventsimple/event.rb', line 50

def skip_dispatcher
  @skip_dispatcher || false
end