Skip to content

Commit

Permalink
sinatra-namespace: Allow for set :<engine>
Browse files Browse the repository at this point in the history
This allows to set render engine options local to a namespace
  • Loading branch information
Michishige Kaito authored and jkowens committed Mar 18, 2020
1 parent 32f416c commit 7de9bd2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sinatra-contrib/lib/sinatra/namespace.rb
Expand Up @@ -225,6 +225,12 @@ module NamespacedMethods
include SharedMethods
attr_reader :base, :templates

ALLOWED_ENGINES = [
:erb, :erubis, :haml, :builder, :nokogiri, :sass, :scss, :less, :liquid,
:markdown, :textile, :rdoc, :asciidoc, :radius, :markaby, :rabl, :slim,
:creole, :mediawiki, :coffee, :stylus, :yajl, :wlang
]

def self.prefixed(*names)
names.each { |n| define_method(n) { |*a, &b| prefixed(n, *a, &b) }}
end
Expand Down Expand Up @@ -279,7 +285,7 @@ def respond_to(*args)
end

def set(key, value = self, &block)
raise ArgumentError, "may not set #{key}" if key != :views
raise ArgumentError, "may not set #{key}" unless ([:views] + ALLOWED_ENGINES).include?(key)
return key.each { |k,v| set(k, v) } if block.nil? and value == self
block ||= proc { value }
singleton_class.send(:define_method, key, &block)
Expand Down
10 changes: 10 additions & 0 deletions sinatra-contrib/spec/namespace_spec.rb
Expand Up @@ -788,4 +788,14 @@ class CError < StandardError;
expect(last_response.body).to eq('true')
end
end

it 'forbids unknown engine settings' do
expect {
mock_app do
namespace '/foo' do
set :unknownsetting
end
end
}.to raise_error(ArgumentError, 'may not set unknownsetting')
end
end

0 comments on commit 7de9bd2

Please sign in to comment.