From 05036dee88672f3f28db57d65eed3753f902f975 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Mon, 25 Nov 2024 09:35:57 -0300 Subject: [PATCH] chore: extend sitemap builder instead of adapter --- README.md | 14 +++++------ lib/site_maps/adapters/adapter.rb | 7 +++--- lib/site_maps/runner.rb | 1 + spec/site_maps/adapters/adapter_spec.rb | 6 ++--- spec/site_maps/runner_spec.rb | 31 +++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 098eb3d..3f199a1 100644 --- a/README.md +++ b/README.md @@ -596,7 +596,7 @@ SiteMaps.generate(config_file: "config/sitemap.rb") ## Mixins -You can use mixins to extend the sitemap builder with additional methods. The mixins can be used to define common methods that will be used in multiple processes. Make sure they are thread-safe, otherwise I recommend to define them in the process block. +You can use mixins to extend the sitemap builder with additional methods. The mixins can be used to define common methods that will be used in multiple processes. ```ruby module MyMixin @@ -610,10 +610,10 @@ module MyMixin end SiteMaps.use(:file_system) do - include_module(MyMixin) + extend_processes_with(MyMixin) process do |s| - repository.posts.each do |post| - s.add(post_path(post), priority: 0.8) + s.repository.posts.each do |post| + s.add(s.post_path(post), priority: 0.8) end end end @@ -622,11 +622,9 @@ end We already have a built-in mixin for Rails applications that provides the url helpers through the `route` method. ```ruby -SiteMaps.use(:file_system) do - include_module(SiteMaps::Mixins::Rails) process do |s| - s.add(route.root_path, priority: 1.0) - s.add(route.about_path, priority: 0.9) + s.add(s.route.root_path, priority: 1.0) + s.add(s.route.about_path, priority: 0.9) end end ``` diff --git a/lib/site_maps/adapters/adapter.rb b/lib/site_maps/adapters/adapter.rb index 0286801..d2844c2 100644 --- a/lib/site_maps/adapters/adapter.rb +++ b/lib/site_maps/adapters/adapter.rb @@ -13,11 +13,12 @@ def config_class end def_delegators :config, :fetch_sitemap_index_links - attr_reader :sitemap_index, :processes + attr_reader :sitemap_index, :processes, :process_mixins def initialize(**options, &block) @config = SiteMaps.config.becomes(self.class.config_class, **options) @processes = Concurrent::Hash.new + @process_mixins = Concurrent::Array.new reset! instance_exec(&block) if block end @@ -68,8 +69,8 @@ def repo @repo ||= SiteMaps::AtomicRepository.new(config.url) end - def include_module(mod) - extend(mod) + def extend_processes_with(mod) + @process_mixins << mod end def reset! diff --git a/lib/site_maps/runner.rb b/lib/site_maps/runner.rb index 1d86693..632ca79 100644 --- a/lib/site_maps/runner.rb +++ b/lib/site_maps/runner.rb @@ -64,6 +64,7 @@ def run location: process.location(**kwargs), notification_payload: { process: process } ) + adapter.process_mixins.each { |mixin| builder.extend(mixin) } process.call(builder, **kwargs) builder.finalize! end diff --git a/spec/site_maps/adapters/adapter_spec.rb b/spec/site_maps/adapters/adapter_spec.rb index 477643c..6b43721 100644 --- a/spec/site_maps/adapters/adapter_spec.rb +++ b/spec/site_maps/adapters/adapter_spec.rb @@ -197,7 +197,7 @@ end end - describe "#include_module" do + describe "#extend_processes_with" do let(:adapter) { described_class.new } let(:mod) do Module.new do @@ -208,8 +208,8 @@ def foo end it "includes the module" do - adapter.include_module(mod) - expect(adapter).to respond_to(:foo) + adapter.extend_processes_with(mod) + expect(adapter.process_mixins).to include(mod) end end end diff --git a/spec/site_maps/runner_spec.rb b/spec/site_maps/runner_spec.rb index 7a27773..6770aa5 100644 --- a/spec/site_maps/runner_spec.rb +++ b/spec/site_maps/runner_spec.rb @@ -275,6 +275,37 @@ ) end end + + context "when the adapter has a process mixin" do + let(:adapter) do + SiteMaps.use(:noop) do + config.url = "https://example.com/sitemap.xml" + extend_processes_with(Module.new do + def add_post(year:) + add("/posts/#{year}/index.html") + end + end) + + process(:posts, "posts/%{year}/sitemap.xml", year: 2024) do |s, year:| + s.add_post(year: year) + end + end + end + + it "extends the process with the mixin" do + runner.enqueue(:posts, year: 2024) + + process = adapter.processes[:posts] + allow(process).to receive(:call).and_call_original + + expect { runner.run }.not_to raise_error + + expect(process).to have_received(:call).with( + an_instance_of(SiteMaps::SitemapBuilder), + year: 2024 + ) + end + end end describe "#preload_sitemap_index_links?" do