Browse Source

Introduce a region Aggregate Root

feature/region_center
Bèr Kessels 5 months ago
parent
commit
59d2c5130e

+ 2
- 2
app/aggregates/place.rb View File

@@ -4,7 +4,7 @@ require_relative '../events/place_added.rb'
require_relative '../events/place_updated.rb'
require_relative './place/address.rb'
require_relative './place/place_id.rb'
require_relative './place/region.rb'
require_relative './place/region_slug.rb'

module Hours
module Aggregates
@@ -75,7 +75,7 @@ module Hours
end

def region_builder
@region_builder || Region.new(@payload)
@region_builder || RegionSlug.new(@payload)
end

def address_builder

app/aggregates/place/region.rb → app/aggregates/place/region_slug.rb View File

@@ -5,7 +5,7 @@ require 'babosa'
module Hours
module Aggregates
# Generates a normalized, distinguishable region string.
class Region
class RegionSlug
def initialize(geojson)
@geojson = Hashie.symbolize_keys(geojson)
end

+ 49
- 0
app/aggregates/region.rb View File

@@ -0,0 +1,49 @@
# frozen_string_literal: true

require_relative '../events/region_added.rb'
require_relative './place/place_id.rb'

module Hours
module Aggregates
##
# A +place+ is one of the core elements in the OpenStreetMap data model. It
# consists of a single point in space defined by its latitude, longitude
# and place id.
class Region
include EventSourcery::AggregateRoot

attr_writer :region_builder

apply RegionAdded do |event|
@aggregate_id = event.aggregate_id
end

def add(payload)
@payload = Hashie.symbolize_keys(payload)
fill_payload

apply_event(RegionAdded, aggregate_id: id, body: @payload)

self
end

private

def fill_payload
merge_properties(region_slug: region_builder.region_slug)
end

def merge_properties(append_properties)
@payload[:properties] ||= {}
@payload[:properties].merge!(append_properties)
end

def region_builder
@region_builder || RegionSlug.new(@payload)
end
end
end

class DuplicateError < StandardError
end
end

+ 3
- 3
test/aggregates/place/region_test.rb View File

@@ -1,11 +1,11 @@
# frozen_string_literal: true

require 'test_helper'
require_relative Hours.base_path.join 'app/aggregates/place/region.rb'
require_relative Hours.base_path.join 'app/aggregates/place/region_slug.rb'

describe Hours::Aggregates::Region do
describe Hours::Aggregates::RegionSlug do
subject do
Hours::Aggregates::Region.new(geojson)
Hours::Aggregates::RegionSlug.new(geojson)
end

describe 'with addr:city' do

+ 29
- 0
test/aggregates/region_test.rb View File

@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'test_helper'

describe Hours::Aggregates::Place do
let(:aggregate_id) { SecureRandom.uuid }
let(:events) { [] }
let(:payload) { {} }
let(:region_slug) { 'region' }
let(:region_builder) { Minitest::Mock.new }
subject { Hours::Aggregates::Region.new(aggregate_id, events) }

before do
region_builder.expect(:region_slug, region_slug)
subject.region_builder = region_builder
end

it 'builds a region_slug' do
region = subject.add(payload)
assert_equal region_slug, body_properties(region)['region_slug']
assert_mock region_builder
end

private

def body_properties(place)
place.changes.first&.body&.fetch('properties', {})
end
end

Loading…
Cancel
Save