Skip to content

Commit

Permalink
Extract logic for checking attribute declaration syntax to separate c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
joshuaclayton committed May 6, 2012
1 parent e3f3498 commit 8e0d492
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 27 additions & 5 deletions lib/factory_girl/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,33 @@ def alias_for?(attr)
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
Expand Down
2 changes: 1 addition & 1 deletion spec/factory_girl/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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)
Expand Down

0 comments on commit 8e0d492

Please sign in to comment.