From 087d13cdac7ba0034e8b056ecc0ed957adc7b8ac Mon Sep 17 00:00:00 2001 From: Nic Boie <boie0025@gmail.com> Date: Wed, 15 Oct 2014 14:21:49 -0500 Subject: [PATCH] Not allow hash syntax setting --- lib/her/model/attributes.rb | 4 ++-- spec/model/attributes_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/her/model/attributes.rb b/lib/her/model/attributes.rb index ea4e79ce..c8622ad9 100644 --- a/lib/her/model/attributes.rb +++ b/lib/her/model/attributes.rb @@ -71,7 +71,7 @@ def self.use_setter_methods(model, params) # # @private def method_missing(method, *args, &blk) - if method.to_s =~ /[?=]$/ || @attributes.include?(method) + if method.to_s =~ /[^\[\]][?=]$/ || @attributes.include?(method) # Extract the attribute attribute = method.to_s.sub(/[?=]$/, '') @@ -87,7 +87,7 @@ def method_missing(method, *args, &blk) # @private def respond_to_missing?(method, include_private = false) - method.to_s.end_with?('=') || method.to_s.end_with?('?') || @attributes.include?(method) || super + method.to_s =~ /[^\[\]][?=]$/ || method.to_s.end_with?('?') || @attributes.include?(method) || super end # Assign new attributes to a resource diff --git a/spec/model/attributes_spec.rb b/spec/model/attributes_spec.rb index 997fc021..8cdfcf9c 100644 --- a/spec/model/attributes_spec.rb +++ b/spec/model/attributes_spec.rb @@ -65,6 +65,18 @@ @new_user.get_attribute(:unknown_method_for_a_user).should be_nil @new_user.get_attribute(:'life-span').should == '3 years' end + + it "does not try to handle hash syntax setter" do + @new_user = Foo::User.new + expect { @new_user[:fullname] = "Tobias Fünke" }.to_not raise_error(ArgumentError) + expect { @new_user[:fullname] = "Tobias Fünke" }.to raise_error(NoMethodError) + end + + it "does not respond to hash syntax setter method" do + @new_user = Foo::User.new + expect(@new_user).to_not respond_to(:[]=) + end + end