⌚ OpenOpeningstijden main server application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node.rb 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. module Hours
  3. module Aggregates
  4. ##
  5. # A +node+ is one of the core elements in the OpenStreetMap data model. It
  6. # consists of a single point in space defined by its latitude, longitude
  7. # and node id.
  8. class Node
  9. include EventSourcery::AggregateRoot
  10. # These apply methods are the hook that this aggregate uses to update
  11. # its internal state from events.
  12. apply NodeAdded do |event|
  13. # We track the ID when a node is added so we can ensure the same node
  14. # isn't added twice.
  15. #
  16. # We can save more attributes off the event in here as necessary.
  17. @aggregate_id = event.aggregate_id
  18. end
  19. def add(payload)
  20. raise UnprocessableEntity, "Node #{id.inspect} already exists" if added?
  21. apply_event(NodeAdded, aggregate_id: id, body: payload)
  22. end
  23. # The methods below are how this aggregate handles different commands.
  24. # Note how they raise new events to indicate the change in state.
  25. private
  26. def added?
  27. @aggregate_id
  28. end
  29. end
  30. end
  31. end