Renamed #attributes to #attributes_for
This commit is contained in:
parent
4985bea035
commit
7dcb2241c6
|
@ -98,7 +98,7 @@ class Factory
|
||||||
# Returns:
|
# Returns:
|
||||||
# A set of attributes that can be used to build an instance of the class
|
# A set of attributes that can be used to build an instance of the class
|
||||||
# this factory generates. (Hash)
|
# this factory generates. (Hash)
|
||||||
def attributes (attrs = {})
|
def attributes_for (attrs = {})
|
||||||
result = {}
|
result = {}
|
||||||
@lazy_attributes.each do |name, block|
|
@lazy_attributes.each do |name, block|
|
||||||
result[name] = block.call unless attrs.key?(name)
|
result[name] = block.call unless attrs.key?(name)
|
||||||
|
@ -112,14 +112,14 @@ class Factory
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# attrs: (Hash)
|
# attrs: (Hash)
|
||||||
# See attributes
|
# See attributes_for
|
||||||
#
|
#
|
||||||
# Returns:
|
# Returns:
|
||||||
# An instance of the class this factory generates, with generated
|
# An instance of the class this factory generates, with generated
|
||||||
# attributes assigned.
|
# attributes assigned.
|
||||||
def build (attrs = {})
|
def build (attrs = {})
|
||||||
instance = build_class.new
|
instance = build_class.new
|
||||||
attributes(attrs).each do |attr, value|
|
attributes_for(attrs).each do |attr, value|
|
||||||
instance.send(:"#{attr}=", value)
|
instance.send(:"#{attr}=", value)
|
||||||
end
|
end
|
||||||
instance
|
instance
|
||||||
|
@ -134,7 +134,7 @@ class Factory
|
||||||
#
|
#
|
||||||
# Arguments:
|
# Arguments:
|
||||||
# attrs: (Hash)
|
# attrs: (Hash)
|
||||||
# See attributes
|
# See attributes_for
|
||||||
#
|
#
|
||||||
# Returns:
|
# Returns:
|
||||||
# A saved instance of the class this factory generates, with generated
|
# A saved instance of the class this factory generates, with generated
|
||||||
|
@ -147,8 +147,8 @@ class Factory
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
def attributes (name, attrs = {})
|
def attributes_for (name, attrs = {})
|
||||||
factory_by_name(name).attributes(attrs)
|
factory_by_name(name).attributes_for(attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build (name, attrs = {})
|
def build (name, attrs = {})
|
||||||
|
|
|
@ -77,7 +77,7 @@ class FactoryTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "include that value in the generated attributes hash" do
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -97,14 +97,14 @@ class FactoryTest < Test::Unit::TestCase
|
||||||
@factory.add_attribute(@attr) do
|
@factory.add_attribute(@attr) do
|
||||||
called = true
|
called = true
|
||||||
end
|
end
|
||||||
@factory.attributes
|
@factory.attributes_for
|
||||||
assert called
|
assert called
|
||||||
end
|
end
|
||||||
|
|
||||||
should "use the result of the block as the value of the attribute" do
|
should "use the result of the block as the value of the attribute" do
|
||||||
value = "Watch out for snakes!"
|
value = "Watch out for snakes!"
|
||||||
@factory.add_attribute(@attr) { value }
|
@factory.add_attribute(@attr) { value }
|
||||||
assert_equal value, @factory.attributes[@attr]
|
assert_equal value, @factory.attributes_for[@attr]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -113,7 +113,7 @@ class FactoryTest < Test::Unit::TestCase
|
||||||
@attr = :first_name
|
@attr = :first_name
|
||||||
@value = 'Sugar'
|
@value = 'Sugar'
|
||||||
@factory.send(@attr, @value)
|
@factory.send(@attr, @value)
|
||||||
assert_equal @value, @factory.attributes[@attr]
|
assert_equal @value, @factory.attributes_for[@attr]
|
||||||
end
|
end
|
||||||
|
|
||||||
should "not allow attributes to be added with both a value parameter and a block" do
|
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
|
should "return the overridden value in the generated attributes" do
|
||||||
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
|
@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
|
end
|
||||||
|
|
||||||
should "not call a lazy attribute block for an overridden attribute" do
|
should "not call a lazy attribute block for an overridden attribute" do
|
||||||
@factory.add_attribute(@attr) { flunk }
|
@factory.add_attribute(@attr) { flunk }
|
||||||
@factory.attributes(@hash)
|
@factory.attributes_for(@hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -227,7 +227,7 @@ class FactoryTest < Test::Unit::TestCase
|
||||||
@factory = Factory.factories[@name]
|
@factory = Factory.factories[@name]
|
||||||
end
|
end
|
||||||
|
|
||||||
[:build, :create, :attributes].each do |method|
|
[:build, :create, :attributes_for].each do |method|
|
||||||
|
|
||||||
should "delegate the #{method} method to the factory instance" do
|
should "delegate the #{method} method to the factory instance" do
|
||||||
@factory.expects(method).with(@attrs)
|
@factory.expects(method).with(@attrs)
|
||||||
|
|
Loading…
Reference in New Issue