1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Extract logic for checking attribute declaration syntax to separate class

This commit is contained in:
Joshua Clayton 2012-05-05 20:08:29 -04:00
parent e3f3498a58
commit 8e0d492e6c
2 changed files with 28 additions and 6 deletions

View file

@ -29,11 +29,33 @@ module FactoryGirl
private
def ensure_non_attribute_writer!
if @name.to_s =~ /=$/
attribute_name = $`
raise AttributeDefinitionError,
"factory_girl uses 'f.#{attribute_name} value' syntax " +
"rather than 'f.#{attribute_name} = value'"
NonAttributeWriterValidator.new(@name).validate!
end
class NonAttributeWriterValidator
def initialize(method_name)
@method_name = method_name.to_s
@method_name_setter_match = @method_name.match(/(.*)=$/)
end
def validate!
if method_is_writer?
raise AttributeDefinitionError, error_message
end
end
private
def method_is_writer?
!!@method_name_setter_match
end
def attribute_name
@method_name_setter_match[1]
end
def error_message
"factory_girl uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'"
end
end
end

View file

@ -8,7 +8,7 @@ describe FactoryGirl::Attribute do
it { should_not be_association }
it "raises an error when defining an attribute writer" do
error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
error_message = %{factory_girl uses 'test value' syntax rather than 'test = value'}
expect {
FactoryGirl::Attribute.new('test=', false)
}.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)