Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ged/bb41737fa45649231985 to your computer and use it in GitHub Desktop.
Save ged/bb41737fa45649231985 to your computer and use it in GitHub Desktop.
diff --git a/lib/inversion/template.rb b/lib/inversion/template.rb
--- a/lib/inversion/template.rb
+++ b/lib/inversion/template.rb
@@ -121,6 +121,7 @@ class Inversion::Template
:ignore_unknown_tags => true,
:template_paths => [],
:stat_delay => 0,
+ :strict_attributes => false,
# Rendering options
:on_render_error => :comment,
@@ -391,6 +392,10 @@ class Inversion::Template
def method_missing( sym, *args, &block )
return super unless sym.to_s =~ /^([a-z]\w+)=?$/i
attribute = $1
+
+ raise NoMethodError, "no tag attribute '%s' (strict mode)" % [ attribute ] if
+ self.options[:strict_attributes]
+
self.install_accessors( attribute )
# Call the new method via #method to avoid a method_missing loop.
diff --git a/spec/inversion/template_spec.rb b/spec/inversion/template_spec.rb
--- a/spec/inversion/template_spec.rb
+++ b/spec/inversion/template_spec.rb
@@ -65,6 +65,7 @@ describe Inversion::Template do
tmpl.render( &renderblock )
end
+
it "carries its global configuration to the parser" do
begin
orig_config = described_class.config
@@ -78,6 +79,7 @@ describe Inversion::Template do
end
end
+
it "carries its global configuration to per-template options" do
begin
orig_config = described_class.config
@@ -103,6 +105,7 @@ describe Inversion::Template do
expect( tmpl.inspect ).to_not match( /node_tree/ )
end
+
it "includes the node tree in the inspected object if debugging is enabled" do
begin
debuglevel = $DEBUG
@@ -115,12 +118,21 @@ describe Inversion::Template do
end
end
+
it "provides accessors for attributes that aren't identifiers in the template" do
tmpl = described_class.new( '' )
tmpl.foo = :bar
expect( tmpl.foo ).to eq( :bar )
end
+
+ it "raises instead of generating an accessor if configured with strict attributes" do
+ tmpl = described_class.new( '', strict_attributes: true )
+ expect { tmpl.foo = :bar }.to raise_error( NoMethodError, "no tag attribute 'foo' (strict mode)" )
+ expect { tmpl.foo }.to raise_error( NoMethodError, "no tag attribute 'foo' (strict mode)")
+ end
+
+
it "can pass an encoding option to IO.open through the template constructor" do
content = 'some stuff'.encode( 'utf-8' )
expect( IO ).to receive( :read ).with( '/a/utf8/template.tmpl', encoding: 'utf-8' ).and_return( content )
@@ -270,7 +282,6 @@ describe Inversion::Template do
end
-
context "without template paths set" do
before( :each ) do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment