Mirosław Pragłowski bio photo

Mirosław Pragłowski

Email Twitter Github
Facebook LinkedIn Instagram

require ‘aggregate_root’

module AccessControl EntryRegistered = Class.new(RailsEventStore::Event) EntryRevoked = Class.new(RailsEventStore::Event) EntryScanned = Class.new(RailsEventStore::Event)

class Entry include AggregateRoot NoEntrance = Class.new(StandardError)

def register(barcode, event_id)
  apply(EntryRegistered.new(data: {barcode: barcode, event_id: event_id}))
end

def revoke
  apply(EntryRevoked.new(data: {barcode: barcode, event_id: event_id}))
end

def scan(terminal)
  raise NoEntrance unless valid?
  apply(EntryScanned.new(data: { barcode: barcode, event_id: event_id,
                                 terminal: terminal }))
end

private
attr_reader :barcode, :event_id, :state

def valid?
  state == :valid
end

def apply_entry_registered(domain_event)
  @barcode = domain_event.data[:barcode]
  @event_id = domain_event.data[:event_id]
  @state = :valid
end

def apply_entry_revoked(_)
  @state = :revoked
end

def apply_entry_scanned(_)
  @state = :scanned
end   end end