Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/sitemap_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ module SitemapGenerator
}

# Lazy-initialize the LinkSet instance
Sitemap = (Class.new do
def method_missing(*args, &block)
(@link_set ||= reset!).send(*args, &block)
Sitemap = (Config = Class.new do
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giving the anonymous class a name here to enhance debug-ability. Looking at classes in the inheritance hierarchy without names is difficult to trace and understand.

Open to alternatives besides SitemapGenerator::Config, though.

# Use a new LinkSet instance
def reset!
@link_set = LinkSet.new
end

def respond_to?(name, include_private = false)
(@link_set ||= reset!).respond_to?(name, include_private)
private

def method_missing(name, *args, &block)
@link_set ||= reset!
@link_set.respond_to?(name, true) ? @link_set.__send__(name, *args, &block) : super
end

# Use a new LinkSet instance
def reset!
@link_set = LinkSet.new
def respond_to_missing?(name, include_private = false)
(@link_set ||= reset!).respond_to?(name, include_private) || super
end
end).new
end
Expand Down
1 change: 1 addition & 0 deletions sitemap_generator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Gem::Specification.new do |s|
s.name = 'sitemap_generator'
s.version = File.read('VERSION').chomp
s.platform = Gem::Platform::RUBY
s.required_ruby_version = '>= 2.6'
s.authors = ['Karl Varga']
s.email = 'kjvarga@gmail.com'
s.homepage = 'https://github.com/kjvarga/sitemap_generator'
Expand Down
37 changes: 37 additions & 0 deletions spec/sitemap_generator/sitemap_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

RSpec.describe SitemapGenerator::Sitemap do
subject { described_class }

it "has a class name" do
expect(subject.class.name).to match "SitemapGenerator::"
end

describe "method missing" do
it "should not be public" do
expect(subject.methods).to_not include :method_missing
end

it "responds properly" do
expect(subject.method :default_host).to be_a Method
end

it "respects inheritance" do
subject.class.include Module.new {
def method_missing(*args)
:inherited
end
def respond_to_missing?(name, *)
name == :something_inherited
end
}

expect(subject).to respond_to :something_inherited
expect(subject.linkset_doesnt_know).to be :inherited
end

it "unconventionally delegates private (and protected) methods" do
expect { subject.options_for_group({}) }.to_not raise_error
end
end
end