Module: Eventsimple::Event::InstanceMethods

Defined in:
lib/eventsimple/event.rb

Instance Method Summary collapse

Instance Method Details

#aggregateObject



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

def aggregate
  public_send(_aggregate_klass.model_name.element)
end

#aggregate=(aggregate) ⇒ Object



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

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

#apply(aggregate) ⇒ Object



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

def apply(aggregate); end

#apply_and_persistObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/eventsimple/event.rb', line 122

def apply_and_persist
  apply_timestamps(aggregate)
  apply(aggregate)

  aggregate.enable_writes!
  aggregate.save!
  aggregate.readonly!

  self.aggregate = aggregate
end

#apply_timestamps(aggregate) ⇒ Object



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

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

#can_apply?(_aggregate) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_apply?(_aggregate)
  true
end

#deleted?Boolean

Returns:

  • (Boolean)


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

def deleted?
  false
end

#dispatchObject



133
134
135
# File 'lib/eventsimple/event.rb', line 133

def dispatch
  EventDispatcher.dispatch(self) unless skip_dispatcher
end

#enable_writes! {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



145
146
147
148
149
150
151
152
153
# File 'lib/eventsimple/event.rb', line 145

def enable_writes!(&block)
  was_readonly = @readonly
  @readonly = false

  return unless block

  yield self
  @readonly = was_readonly
end

#extend_validationObject



117
118
119
120
# File 'lib/eventsimple/event.rb', line 117

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)


106
107
108
109
110
111
112
113
114
115
# File 'lib/eventsimple/event.rb', line 106

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



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

def skip_apply_check
  @skip_apply_check || false
end

#skip_dispatcherObject



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

def skip_dispatcher
  @skip_dispatcher || false
end