Module: Eventsimple::Entity::InstanceMethods

Defined in:
lib/eventsimple/entity.rb

Instance Method Summary collapse

Instance Method Details

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

Yields:

  • (_self)

Yield Parameters:



59
60
61
62
63
64
65
66
67
# File 'lib/eventsimple/entity.rb', line 59

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

  return unless block

  yield self
  @readonly = was_readonly
end

#projection_matches_events?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/eventsimple/entity.rb', line 53

def projection_matches_events?
  reprojected = self.class.unscoped.find(id).reproject

  attributes == reprojected.attributes
end

#reproject(at: nil, skip_deleted: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/eventsimple/entity.rb', line 69

def reproject(at: nil, skip_deleted: false)
  event_history = at ? events.where('created_at <= ?', at).load : events.load

  if !skip_deleted && event_history.any?(&:deleted?)
    raise "Cannot reproject: deleted event(s) found in event history"
  end

  ignore_props = (DEFAULT_IGNORE_PROPS + ignored_for_projection).map(&:to_s)
  assign_attributes(self.class.column_defaults.except(*ignore_props))

  event_history.each do |event|
    event.apply_timestamps(self)
    event.apply(self)
  end

  self
end