⌚ 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.

place.rb 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # frozen_string_literal: true
  2. require_relative '../events/place_added.rb'
  3. require_relative './place/place_id.rb'
  4. require_relative './place/region.rb'
  5. module Hours
  6. module Aggregates
  7. ##
  8. # A +place+ is one of the core elements in the OpenStreetMap data model. It
  9. # consists of a single point in space defined by its latitude, longitude
  10. # and place id.
  11. class Place
  12. include EventSourcery::AggregateRoot
  13. UUID_PLACE_NAMESPACE = UUIDTools::UUID.parse(
  14. '99586308-6dcd-489a-a9dd-9c59cf3feb59'
  15. )
  16. def self.aggregate_id_for(payload)
  17. UUIDTools::UUID.sha1_create(UUID_PLACE_NAMESPACE,
  18. PlaceId.new(payload).id)
  19. end
  20. attr_writer :place_id_builder, :region_builder
  21. attr_reader :place_id, :region_slug
  22. # These apply methods are the hook that this aggregate uses to update
  23. # its internal state from events.
  24. apply PlaceAdded do |event|
  25. @aggregate_id = event.aggregate_id
  26. end
  27. def add(payload)
  28. raise DuplicateError, "Place #{id.inspect} already exists" if added?
  29. @payload = Hashie.symbolize_keys(payload)
  30. @place_id = place_id_builder.id
  31. @region_slug = region_builder.region_slug
  32. apply_event(PlaceAdded, aggregate_id: id, body: event_body)
  33. self
  34. end
  35. private
  36. def added?
  37. @aggregate_id
  38. end
  39. def event_body
  40. @payload.merge(
  41. properties: properties.merge(
  42. place_id: place_id,
  43. region_slug: region_slug,
  44. 'addr:city': city
  45. )
  46. )
  47. end
  48. def properties
  49. @payload.fetch(:properties, {})
  50. end
  51. def place_id_builder
  52. @place_id_builder || PlaceId.new(@payload)
  53. end
  54. def region_builder
  55. @region_builder || Region.new(@payload)
  56. end
  57. def city
  58. properties[:'addr:city'] || region_builder.city
  59. end
  60. end
  61. end
  62. class DuplicateError < StandardError
  63. end
  64. end