Module: Eventsimple::Event::InstanceMethods

Defined in:
lib/eventsimple/event.rb

Instance Method Summary collapse

Instance Method Details

#aggregateObject



141
142
143
# File 'lib/eventsimple/event.rb', line 141

def aggregate
  public_send(_aggregate_klass.model_name.element)
end

#aggregate=(aggregate) ⇒ Object



145
146
147
# File 'lib/eventsimple/event.rb', line 145

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

#apply(aggregate) ⇒ Object



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

def apply(aggregate); end

#apply_and_persistObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/eventsimple/event.rb', line 125

def apply_and_persist
  apply_timestamps(aggregate)
  apply(aggregate)

  Eventsimple.enable_writes! do
    aggregate.save!
  end
  aggregate.readonly!

  self.aggregate = aggregate
end

#apply_timestamps(aggregate) ⇒ Object



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

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

#can_apply?(_aggregate) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_apply?(_aggregate)
  true
end

#deleted?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/eventsimple/event.rb', line 94

def deleted?
  false
end

#dispatchObject



137
138
139
# File 'lib/eventsimple/event.rb', line 137

def dispatch
  EventDispatcher.dispatch(self) unless skip_dispatcher
end

#extend_validationObject



120
121
122
123
# File 'lib/eventsimple/event.rb', line 120

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)


109
110
111
112
113
114
115
116
117
118
# File 'lib/eventsimple/event.rb', line 109

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



90
91
92
# File 'lib/eventsimple/event.rb', line 90

def skip_apply_check
  @skip_apply_check || false
end

#skip_dispatcherObject



86
87
88
# File 'lib/eventsimple/event.rb', line 86

def skip_dispatcher
  @skip_dispatcher || false
end