Renamed #attributes to #attributes_for

This commit is contained in:
Joe Ferris 2008-05-28 22:22:48 -04:00
parent 4985bea035
commit 7dcb2241c6
2 changed files with 13 additions and 13 deletions

View File

@ -98,7 +98,7 @@ class Factory
# Returns:
# A set of attributes that can be used to build an instance of the class
# this factory generates. (Hash)
def attributes (attrs = {})
def attributes_for (attrs = {})
result = {}
@lazy_attributes.each do |name, block|
result[name] = block.call unless attrs.key?(name)
@ -112,14 +112,14 @@ class Factory
#
# Arguments:
# attrs: (Hash)
# See attributes
# See attributes_for
#
# Returns:
# An instance of the class this factory generates, with generated
# attributes assigned.
def build (attrs = {})
instance = build_class.new
attributes(attrs).each do |attr, value|
attributes_for(attrs).each do |attr, value|
instance.send(:"#{attr}=", value)
end
instance
@ -134,7 +134,7 @@ class Factory
#
# Arguments:
# attrs: (Hash)
# See attributes
# See attributes_for
#
# Returns:
# A saved instance of the class this factory generates, with generated
@ -147,8 +147,8 @@ class Factory
class << self
def attributes (name, attrs = {})
factory_by_name(name).attributes(attrs)
def attributes_for (name, attrs = {})
factory_by_name(name).attributes_for(attrs)
end
def build (name, attrs = {})

View File

@ -77,7 +77,7 @@ class FactoryTest < Test::Unit::TestCase
end
should "include that value in the generated attributes hash" do
assert_equal @value, @factory.attributes[@attr]
assert_equal @value, @factory.attributes_for[@attr]
end
end
@ -97,14 +97,14 @@ class FactoryTest < Test::Unit::TestCase
@factory.add_attribute(@attr) do
called = true
end
@factory.attributes
@factory.attributes_for
assert called
end
should "use the result of the block as the value of the attribute" do
value = "Watch out for snakes!"
@factory.add_attribute(@attr) { value }
assert_equal value, @factory.attributes[@attr]
assert_equal value, @factory.attributes_for[@attr]
end
end
@ -113,7 +113,7 @@ class FactoryTest < Test::Unit::TestCase
@attr = :first_name
@value = 'Sugar'
@factory.send(@attr, @value)
assert_equal @value, @factory.attributes[@attr]
assert_equal @value, @factory.attributes_for[@attr]
end
should "not allow attributes to be added with both a value parameter and a block" do
@ -132,12 +132,12 @@ class FactoryTest < Test::Unit::TestCase
should "return the overridden value in the generated attributes" do
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
assert_equal @value, @factory.attributes(@hash)[@attr]
assert_equal @value, @factory.attributes_for(@hash)[@attr]
end
should "not call a lazy attribute block for an overridden attribute" do
@factory.add_attribute(@attr) { flunk }
@factory.attributes(@hash)
@factory.attributes_for(@hash)
end
end
@ -227,7 +227,7 @@ class FactoryTest < Test::Unit::TestCase
@factory = Factory.factories[@name]
end
[:build, :create, :attributes].each do |method|
[:build, :create, :attributes_for].each do |method|
should "delegate the #{method} method to the factory instance" do
@factory.expects(method).with(@attrs)